本文介绍了强制ThreadPool线程的行为就像它们不是后台线程一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

您好


我已经编程C#几个月了,我正在尝试

主线程。


我明白ThreadPool使用后台线程(参见MSDN页面中的代码示例

标题为''ThreadPool类[C#]'')....但是我想要

确保我退出之前所有的ThreadPool线程都已完成

我的主要功能。


目前我在主要代码中使用此代码功能:

ThreadPool.GetMaxThreads(输出maxThreads,输出

maxPortCompletionThreads);

do

{

Thread.Sleep(100);

ThreadPool.GetAvailableThreads(输出workerThreads,输出

portCompletionThreads);

} while( maxThreads!= workerThreads || maxPortCompletio nThreads!= portCompletionThreads);


我在主函数末尾使用上面的代码,它似乎是
似乎确保所有ThreadPool线程完成
(因此应用程序

结束)。


我知道我没有'使用ThreadPool - 我可以启动

我自己的线程然后加入主线程到自己的线程。

但是我宁愿坚持使用ThreadPool我可以。


我的问题很简单''有没有更好的方法来确保所有

ThreadPool线程在退出main函数之前完成?'我现在写的

代码是一个大型项目的一部分,我想要

来使我的贡献相当标准(而不是有其他的

程序员看看我的工作并说''他在这里做了什么?'?

我以前没看过这个!)...


预先谢谢

Bill

Hi There

I have been programming C# for a couple of months and am trying to
master Threading.

I understand that ThreadPool uses background threads (see code example
in MSDN page titled ''ThreadPool Class [C#]'') .... however I would like
to ensure that all my ThreadPool threads have completed before I exit
my Main function.

At the moment I am using this code in my main function:
ThreadPool.GetMaxThreads(out maxThreads,out
maxPortCompletionThreads);
do
{
Thread.Sleep(100);
ThreadPool.GetAvailableThreads(out workerThreads,out
portCompletionThreads);
}while(maxThreads!=workerThreads||maxPortCompletio nThreads!=portCompletionThreads);

I use the above code right at the end of my main function, and it
seems to do the job of ensuring that all ThreadPool threads finish
before the main function completes (and consequently the application
finishes).

I am aware that I do not ''have to'' use the ThreadPool - I could start
my own threads up then join the main thread to the own threads.
However I would prefer to stick to the ThreadPool if I can.

My question is simply ''is there a better way to ensure that all
ThreadPool threads complete before exiting the main function'' ? The
code I am writing at the moment is part of a large project and I want
to make my contribution fairly standard (rather than have other
programmers look at my work and say ''what the heck is he doing here?!?
I have not seen this before!) ...

Thanks In Advance
Bill

推荐答案





这篇关于强制ThreadPool线程的行为就像它们不是后台线程一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 06:00