SmartThreadPool阻止UI线程 [英] SmartThreadPool Blocking UI Thread
问题描述
单击按钮时,我正在运行以下代码:
I am running the following code when i click on a button:
foreach (string item in urlQueue)
{
log("creating job " + iia.ToString());
_smartThreadPool.QueueWorkItem(
new Amib.Threading.Func<string, int, int, string, int>(checkURLSmart),
item, iia, 5000, kryptonTextBox1.Text);
iia++;
}
Application.DoEvents();
_smartThreadPool.Start();
_smartThreadPool.WaitForIdle();
_smartThreadPool.Shutdown();
由于某种原因,这阻塞了UI线程,有什么主意如何解决?我希望用户界面在队列工作时能够响应
For some reason this is blocking the UI thread, any ideas how to fix this? I want the UI to be responsive while the queue is working
推荐答案
您不应在GUI线程中调用WaitForIdle(). 使用 http://msdn.microsoft.com/en-us/library/a06c0dc2 .NET或 http:.aspx //p.docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html Java中的invokeLater().
You shouldn't call WaitForIdle() in GUI thread. Use http://msdn.microsoft.com/en-us/library/a06c0dc2.aspx in .NET or http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingUtilities.html invokeLater() in Java instead.
此机制将您的代码放入队列中,以便在GUI线程中执行.因此,您可以使用回调参数中已处理的数据来更新GUI视图.
This mechanism put your code in queue for execution in GUI thread. So you are able to update GUI view with processed data in callback parameters.
另请参阅有关Invoke&之间的区别的说明. BeginInvoke: Invoke()和BeginInvoke()有什么区别
Also see explanation about differences between Invoke & BeginInvoke: What's the difference between Invoke() and BeginInvoke()
这篇关于SmartThreadPool阻止UI线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!