.NET线程池 - 无响应的WinForms UI [英] .NET Thread Pool - Unresponsive WinForms UI

查看:137
本文介绍了.NET线程池 - 无响应的WinForms UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows窗体应用程序。里面的主要形式有一个循环,围绕3000次迭代,在一个新的线程创建类的新实例进行一些计算。铭记这一设置使用线程池,用户界面​​也时有大约只有100次迭代这个循环的(100资产处理)保持响应。但是,一旦这个数字开始增加巨资,用户界面​​锁定成eggtimer模式,并由此被写入到列表框的形式变得不可读的日志。

I have a Windows Forms Application. Inside the main form there is a loop that iterates around 3000 times, Creating a new instance of a class on a new thread to perform some calculations. Bearing in mind that this setup uses a Thread Pool, the UI does stay responsive when there are only around 100 iterations of this loop (100 Assets to process). But as soon as this number begins to increase heavily, the UI locks up into eggtimer mode and the thus the log that is writing out to the listbox on the form becomes unreadable.

我是正确的思维,围绕这一点的最好办法是使用一个后台工作?
,并锁定了,因为UI即使我使用了大量不同的线程(速度),用户界面​​本身并不在自己独立的线程?

Am I right in thinking that the best way around this is to use a Background Worker? And is the UI locking up because even though I'm using lots of different threads (for speed), the UI itself is not on its own separate thread?

建议的实现极大的赞赏。

Suggested Implementations greatly appreciated.

因此,可以说,与其只是发射了和排队3000资产过程中,我决定做他们的100.我将如何去有效地做这个批次?我做出了尝试在前面加上Thread.sleep代码(5000); 100每批次之后被解雇了,但整个事情似乎废话了......

So lets say that instead of just firing off and queuing up 3000 assets to process, I decide to do them in batches of 100. How would I go about doing this efficiently? I made an attempt earlier at adding "Thread.Sleep(5000);" after every batch of 100 were fired off, but the whole thing seemed to crap out....

推荐答案

更​​多的线程不平等的最高速度。事实上过多线程等于少速度。如果您应该只使用尽可能多线程你的任务仅仅是CPU与你有核心否则你就是在浪费资源。

More threads does not equal top speed. In fact too many threads equals less speed. If your task is simply CPU related you should only be using as many threads as you have cores otherwise you're wasting resources.

<击>随着3000迭代和表单线程尝试每次什么可能发生的事情是您杏线程池和形式挂,因为它创建线程需要等待一个前线程来完成,才可以分配一个新的。

显然线程池不以这种方式工作。我从来没有与线程检查之前,所以我不知道。另一种可能性是,任务开始调用此时它会放弃对GUI泛滥的UI线程。

Apparently ThreadPool doesn't work this way. I have never checked it with threads before so I am not sure. Another possibility is that the tasks begin flooding the UI thread with invocations at which point it will give up on the GUI.

这篇关于.NET线程池 - 无响应的WinForms UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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