如何不阻止窗口形式的循环 [英] how to not blocking loop in a window form

查看:67
本文介绍了如何不阻止窗口形式的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我在.net编程中还很新,我想得到一些建议.

我正在尝试使用VS2010 Designer创建一个简单的客户端GUI应用程序,以创建一个我具有的单一表单:
1个comboBox,(包含可能的命令列表);
1个按钮,(用于执行在组合框中选择的命令);
1个图片框(在其中显示从服务器应用程序收到的图像);

每次按下按钮,我都可以创建我的客户端应用程序,并在从服务器收到的图片框中显示不同的图像.

我想做的是在事件按钮单击中不阻塞循环,以便只要将客户端组合框命令设置为开始成像,服务器发送的图像就会显示在图片框中,当客户端组合框停止时它将停止命令设置为停止成像.

我不确定该怎么做,因为如果我尝试在事件按钮单击中循环,则GUI将变得无响应,并且我没有机会在组合框中更改命令.

任何帮助将不胜感激.
谢谢.

Dear all,

i''m pretty new in the .net programming and i would like to get some suggestions.

I''m trying to create an easy client GUI application using the VS2010 Designer to create a single form in which i have:
1 comboBox, (containing the list of possible commands);
1 button, (used to execute the command selected in the combobox);
1 picturebox (in which i display images received from my server application);

I was able to create my client application and display a different image in the picturebox received from the server everytime i press the button.

What i would like to do is a not blocking loop in the event button click so that as long as the the client combobox command is set to start imaging, the images sent by the server are displayed in the picturebox and it stops when the client combobox command is set to stop imaging.

I''m not sure about how to do that because if i try and loop in the event button click, the GUI becomes unresponsive and i don''t have a chance to change the command in the combobox.

Any help would be much appreciated.
Thanks.

推荐答案

甚至不要玩弄干预UI周期的想法(这可能是肮脏且无效的,只是一个坏主意). br/>
在所有情况下,请使用具有延迟(System.Threading.Thread.Sleep)的单独线程.在此线程中,定期更改图片.使用以下三种获取线程的方法之一:
http://msdn.microsoft.com/en-us/library/xx3ezzs2.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.threading. threadpool.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.componentmodel. backgroundworker.aspx [ ^ ].

如果您想每次启动此动画"时都创建一个线程,则应使用BackgroundWorker(上面已引用).您也可以从头开始创建一个线程,并使用事件等待句柄对其进行限制,在要启动动画时进行设置,并重置为暂停/停止它.请参阅System.Threading.EventWaitHandleSystem.Threading.ManualResetEvent:
http://msdn.microsoft.com/en-us/library/system.threading. eventwaithandle.aspx [ ^ ].

对于由线程构造函数创建的线程(请参见上面的第一个参考),最好使用我在过去的答案中提供的线程包装器:
如何将ref参数传递给线程 [ ^ ],
启动后更改线程(生产者)的参数 [ ^ ].

现在,问题在于您需要在UI中显示图像的变化.您无法从非UI线程调用与UI相关的任何操作.相反,您需要使用System.Windows.Threading.DispatcherInvokeBeginInvoke方法(对于Forms或WPF)或System.Windows.Forms.Control(仅对于Forms).

在我过去的答案中,您将找到有关其工作原理的详细说明和代码示例:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview扫描仪和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
Don''t even play with the idea of intervention on the UI cycle (it is possible, dirty and ineffective, just a bad idea).

In all cases, use a separate thread with delay (System.Threading.Thread.Sleep). In this thread, change the picture periodically. Use one of these three method of obtaining a thread:
http://msdn.microsoft.com/en-us/library/xx3ezzs2.aspx[^],
http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx[^],
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx[^].

If you want to create a thread each time you want to start this "animation", you should use BackgroundWorker (referenced above). You can also create a thread from the very beginning and throttle it with a event wait handle, set it when you want to start animation, reset to pause/stop it. Please see System.Threading.EventWaitHandle or System.Threading.ManualResetEvent:
http://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.aspx[^].

For the thread created by a thread constructor (see the very first reference above), it''s the best to use a thread wrapper I offered in my past answers:
How to pass ref parameter to the thread[^],
change paramters of thread (producer) after it started[^].

Now, the problem is that you need to show the change in image in UI. You cannot call anything related to UI from non-UI thread. Instead, you need to use the method Invoke or BeginInvoke of System.Windows.Threading.Dispatcher (for both Forms or WPF) or System.Windows.Forms.Control (Forms only).

You will find detailed explanation of how it works and code samples in my past answers:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

See also more references on threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA


这篇关于如何不阻止窗口形式的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆