首页
论坛
新职位
搜索论坛
什么是新的
新职位
新的个人资料帖子
最新活动
会员
目前的访客
新的个人资料帖子
搜索个人资料帖子
VB.NET社区
登录
寄存器
什么是新的
搜索
搜索
仅搜索标题
通过:
新职位
搜索论坛
Menu
Log in
Register
安装应用
安装
首页
论坛
C#
Windows表格
将控制台代码转换成表单应用程序?
您正在使用过期的浏览器。它可能无法正确显示此网站或其他网站。
您应该升级或使用
替代浏览器
.
回复主题
信息
<blockquote data-quote ="Sheepings" data-source="post: 18267" data-attributes="member: 11331"><p>You really shouldn'这样更新您的用户界面。</p><p></p><ul> <li data-xf-list-type ="ul">转到WPF,并跳过Winforms,因为Winforms是EOL。您应该使用WPF或Xamarin。</li> <li data-xf-list-type ="ul">您应该使用模型来控制和更新UI。</li> <li data-xf-list-type ="ul">缺少模型将意味着您当前正在错误地更新UI。无论您的手术量有多小,都不应'不能在用户界面的按钮或其他控件中运行操作。那'这就是为什么我们使用视图和模型。</li> <li data-xf-list-type ="ul">通过不使用模型,您需要其他方法来更新UI。对于那些谁'喜欢看风景;他们通常更喜欢使用线程和任务。虽然可以,但是'不是推荐的方法。视图和模型。</li> </ul><p>However; if you want to simply update your UI, and not have it hanging while it finds all open processes (which it will if their are loads open.) You can use this example code, on how to use threading and tasks to update your UI while performing lengthily operations. While this will also do as you want, and more efficiently. There is still a lot of room for improvements, such as adding using blocks to the tasks, however, you should also be careful doing that, as a using block may also try to dispose of a task which hasn't finished running yet, and that will result in an exception been thrown. There are a lot of modifications you can improve on here, but if you are really interested in going this route instead of WPF and the whole views and models approach, then try searching for keywords such as threading, multithreading, and search by my username. I've answered these questions many times before and I have provided a lot of examples in the past.</p><p></p><p>Here is the code :</p><p>[CODE=csharp] internal static List<Process> processlist = new List<Process>();</p><p> internal delegate void Delegate_Callback_To(string itm_Value, Control control, bool visibility);</p><p> public void UpdateUI_With_NewItem(string value, Control control, bool visibility)</p><p> {</p><p> control.Text = value;</p><p> control.Visible = visibility;</p><p> }</p><p> public Form1()</p><p> {</p><p> InitializeComponent();</p><p> }</p><p> private void button1_Click(object sender, EventArgs e)</p><p> {</p><p> Task Task_UpdateLabel = new Task(() => UpdateLabel("Please wait while we search for open programs", label1, true));</p><p> Task_UpdateLabel.Start();</p><p></p><p> Task FindProcess = new Task(() => GetProcesses());</p><p> FindProcess.Start();</p><p> }</p><p> internal void UpdateLabel(string ui_Message, Control control, bool visible) => Rtxt1.Invoke(new Delegate_Callback_To(UpdateUI_With_NewItem), ui_Message, control, visible);</p><p> internal List<Process> GetProcesses()</p><p> {</p><p> processlist.AddRange(Process.GetProcesses().Where(process => !string.IsNullOrEmpty(process.MainWindowTitle)));</p><p> Task WithProcessList = new Task(() => WithProcesses(processlist));</p><p> WithProcessList.Start();</p><p> WithProcessList.Wait();</p><p> return processlist;</p><p> }</p><p> internal void WithProcesses(List<Process> processes)</p><p> {</p><p> StringBuilder builder = new StringBuilder();</p><p> processes.ForEach((Process current_Process) => builder.AppendLine($"Process: {current_Process.ProcessName} ID: {current_Process.Id} Window title: {current_Process.MainWindowTitle}"));</p><p> Rtxt1.Invoke(new Delegate_Callback_To(UpdateUI_With_NewItem), builder.ToString(), Rtxt1, true);</p><p> label1.Invoke(new Delegate_Callback_To(UpdateUI_With_NewItem), "", label1, false);</p><p> }[/CODE]</p><p></p><p>Some example screenshots of it in action. I also added label1 to display a message to the user while the tasks are running, and once completed the label is set advisable. Feel free to make improvements, and share them here if you wish.</p><p></p><p>Beginning : <a href="http://prntscr.com/udb620">Screenshot</a></p><p>Ending : <a href="http://prntscr.com/udb6d9">Screenshot</a></p></blockquote><p></p>
Insert quotes…
验证
发表回复
首页
论坛
C#
Windows表格
将控制台代码转换成表单应用程序?
本网站使用Cookie来帮助个性化内容,调整您的体验并在注册时保持登录状态。
继续使用本网站,即表示您同意我们使用cookie。
接受
了解更多…
最佳
底部