ThreadPool设置是全局的还是仅针对工作项? [英] Are ThreadPool settings global or only for work items?

查看:278
本文介绍了ThreadPool设置是全局的还是仅针对工作项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在WinForms应用程序中设置了ThreadPool.SetMaxThreads,此设置仅适用于工作项还是适用于整个程序集线程,如计时器和gui更新?

If I set ThreadPool.SetMaxThreads in a WinForms application does this setting applies to only work items or for the whole assembly threads like timers and gui updates?

我正在构建一个应用程序,该应用程序将连续处理半短操作,通常每个操作大约1分钟.我首先想出了一个手动线程创建版本,该版本运行良好.由于我正在为每个操作创建一个新线程,因此我在寻找是否可以创建更快,更高效的版本,并且我尝试了ThreadPool并不能更好地工作,它甚至阻止了我的计时器和gui更新.这是正常现象还是我使用错了?

I'm building an application which will continuously process semi-short operations, usually around 1 minute each. I first came up with a manual thread creating version which works fine. Since I'm creating a new thread for each operation I'm looking if I can create a faster and more efficient version and I experimented with ThreadPool which didn't work better it even blocked my timer and gui updates. Is this normal or am I using it wrong?

这是我的伪代码:

手动线程版本:(所有MaxThreadThreadCount读和写都使用锁进行)

Manual Thread version: (all MaxThread and ThreadCount read and writes are done with locks)

timer1.Tick += Tick();

private void Tick()
{
    //do some text logging
    //do some TextBox updating
}

int MaxThreads = 10

while(true)
{
    if(ThreadCount < MaxThreads)
    {
        new Thread(() => Process()).Start();
        ThreadCount++;
    }
    else
    {
        Thread.Sleep(10000);
    }
}

private void Process()
{
    //do something

    ThreadCount--;
}

ThreadPool版本:(计算机是32位Windows服务器操作系统上的双核Xeon)

ThreadPool version: (the computer is dual-core Xeon on 32bit Windows server OS)

timer1.Tick += Tick();

private void Tick()
{
    //do some text logging
    //do some TextBox updating
}

ThreadPool.SetMaxThreads(10,10) **<--removing this didn't help with freezing at all**

while(true)
{
    if(ThreadPool.GetAvailableThreads() > 0)
    {
        ThreadPool.QueueWorkItem(Process, null)
    }
    else
    {
        Thread.Sleep(10000);
    }
}

private void Process()
{
    //do something
}

推荐答案

根据我对MSDN的研究,限制ThreadPool线程数将影响以下线程机制:

According to my research on MSDN, limiting the number of ThreadPool threads will affect the following threading mechanisms:

  • 线程池
  • System.Threading.Timer
  • System.Timers.Timer
  • TPL任务

因为他们在后台使用线程池来运行他们的工作.
以下将在自己的线程上运行:

since under the hood they are using the thread pool to run their work.
The following will run on their own thread:

  • 线程
  • BackgroundWorker

GUI计时器将在UI线程:System.Windows.Threading.DispatcherTimerSystem.Windows.Forms.Timer上运行.

The GUI timers will run on the UI thread: System.Windows.Threading.DispatcherTimer and System.Windows.Forms.Timer.

这篇关于ThreadPool设置是全局的还是仅针对工作项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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