强制ThreadPool线程的行为就像它们不是后台线程一样 [英] Forcing ThreadPool threads to act like they are not background threads

查看:66
本文介绍了强制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

推荐答案

您应该使用WaitHandle或锁定机制来同步多个线程。 br />

private AutoResetEvent ev = new AutoResetEvent(false);

private delegate void ThreadPooledCallInvoker();


public void ThreadPooledCall()

{

// Todo:东西


//警报等待线程我们已经完成

ev.Set();

}


public void DoStuff()

{

new ThreadPooledCallInvoker(ThreadPooledCall).BeginInv oke(null,null);


//等待后台线程完成f或者最多10秒

if(ev.WaitOne(10000,false))

{

//超时到期!

}

}


-

Dave Sexton
dave @ www..jwaonline..com

------------------- -------------------------------------------------- -

orekin <或******* @ yahoo.com.au>在消息新闻中写道:84 ************************** @ posting.google.c om ...
You should use a WaitHandle or a locking mechanism to synchronize multiple threads.

private AutoResetEvent ev = new AutoResetEvent(false);
private delegate void ThreadPooledCallInvoker();

public void ThreadPooledCall()
{
// Todo: stuff

// Alert waiting threads we are complete
ev.Set();
}

public void DoStuff()
{
new ThreadPooledCallInvoker(ThreadPooledCall).BeginInv oke(null, null);

// wait till the background thread completes for up to 10 seconds
if (ev.WaitOne(10000, false))
{
// timeout expired!
}
}

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"orekin" <or*******@yahoo.com.au> wrote in message news:84**************************@posting.google.c om...
嗨那里

我已经编程C#几个月了,我正在努力掌握线程。

我理解ThreadPool使用后台线程(参见代码示例)
在MSDN页面标题为''ThreadPool类[C#]'')....但是我希望
确保我退出
我的主要功能之前我的所有ThreadPool线程都已完成。

目前我在我的主要功能中使用此代码:
ThreadPool.GetMaxThreads(输出maxThreads,输出
maxPortCompletionThreads);

{
Thread.Sleep(100);
ThreadPool.GetAvailableThreads(输出workerThreads,out
portCompletionThreads);
} while(maxThreads!= workerThreads || maxPortCompletio nThreads!= portCompletionThreads);

我在我的主要功能结束时使用上面的代码,它似乎完成了确保所有功能的工作ThreadPool线程在主函数完成之前完成(因此应用程序完成)。

我知道我不必使用ThreadPool - 我可以启动我自己的线程然后加入主线程到自己的线程。
但是如果可以的话我宁愿坚持使用ThreadPool。

我的问题很简单' '有没有更好的方法来确保所有
ThreadPool线程在退出主函数之前完成?'我现在写的代码是一个大型项目的一部分,我希望
能够使我的贡献相当标准(而不是让其他程序员看看我的工作并说''是什么他到底在这里干什么?!?
我以前没看过这个!)...

先谢谢
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



一个小的 - 你需要给自己一些机制来调用您调用BeginInvoke on的委托实例上的EndInvoke(auch as as AsyncCallback)


问候


Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk


您应该使用WaitHandle或锁定机制来同步多个线程。


private AutoResetEvent ev = new AutoRe setEvent(false);

private delegate void ThreadPooledCallInvoker();


public void ThreadPooledCall()

{

// Todo:东西


//警报等待线程我们已经完成

ev.Set();

}


public void DoStuff()

{

new ThreadPooledCallInvoker(ThreadPooledCall).BeginInv oke(null,null);


//等到后台线程完成最多10秒

if(ev.WaitOne(10000,false))

{

//超时已到期!

}

}


-

Dave Sexton
dave @ www..jwaonline..com

------------------------------------------------- ----------------------

One small nit - you need to give yourself some mechanism for calling EndInvoke on the delegate instance you called BeginInvoke on (auch as passing an AsyncCallback)

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

You should use a WaitHandle or a locking mechanism to synchronize multiple threads.

private AutoResetEvent ev = new AutoResetEvent(false);
private delegate void ThreadPooledCallInvoker();

public void ThreadPooledCall()
{
// Todo: stuff

// Alert waiting threads we are complete
ev.Set();
}

public void DoStuff()
{
new ThreadPooledCallInvoker(ThreadPooledCall).BeginInv oke(null, null);

// wait till the background thread completes for up to 10 seconds
if (ev.WaitOne(10000, false))
{
// timeout expired!
}
}

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------


Hi Dave&理查德


Beta网站有一些错误,所以这可能是第一次出现这个回复

,或者第四次!


感谢您的回复。我可以使用一个线程获得你提供的代码工作

,但是使用AutoResetEvent并且使用超过

一个工作线程时遇到了问题。你怎么看待这个解决方案(你应该是

能够剪切粘贴并编译这段代码)?


使用系统;

使用System.Threading;

使用System.Diagnostics;


命名空间AutoResetEvent_Examples

{

class MyMainClass

{

private delegate void ThreadPooledCallInvoker();


public static void ThreadPooledCall()

{

Debug.WriteLine(来自ThreadPooledCall的Hello!);

Thread.Sleep(500);

}


static void Main()

{

//创建ThreadPooledCallInvoker代表。

ThreadPooledCallInvoker dlgt = new

ThreadPooledCallInvoker(ThreadPooledCall);

ThreadPooledCallInvoker dlgt2 = new

ThreadPooledCallInvoker(ThreadPooledCall);


//让代表去吧

IAsyncResult ar = dlgt.BeginInvoke(null,null);

IAsyncResult ar2 = dlgt2.BeginInvoke(null,null);

Thread.Sleep(0); //允许代表开始


//创建WaitHandleArray

WaitHandle [] myWaiters = new WaitHandle [2];

myWaiters [0] = ar.AsyncWaitHandle;

myWaiters [1] = ar2.AsyncWaitHandle;


if(!WaitHandle.WaitAll(myWaiters,1000,false) ))

{

//超时

Debug.WriteLine(线程超时);

}

else

{

Debug.WriteLine(&#ThreadD Completed");

dlgt.EndInvoke (ar);

dlgt2.EndInvoke(ar2);

}

}

}

}


谢谢

比尔

Hi Dave & Richard

The Beta site is having some errors, so this might be the first time
this reply appears, or the Fourth!

Thanks for your response. I could get the code you supplied working
with one thread, but had problems using AutoResetEvent with more than
one worker thread. What do you think of this solution (you should be
able to cut paste and compile this code)?

using System;
using System.Threading;
using System.Diagnostics;

namespace AutoResetEvent_Examples
{
class MyMainClass
{
private delegate void ThreadPooledCallInvoker();

public static void ThreadPooledCall()
{
Debug.WriteLine("Hello from ThreadPooledCall!");
Thread.Sleep(500);
}

static void Main()
{
// Create the ThreadPooledCallInvoker delegates.
ThreadPooledCallInvoker dlgt = new
ThreadPooledCallInvoker(ThreadPooledCall);
ThreadPooledCallInvoker dlgt2 = new
ThreadPooledCallInvoker(ThreadPooledCall);

// Get the delegates going
IAsyncResult ar = dlgt.BeginInvoke(null,null);
IAsyncResult ar2 = dlgt2.BeginInvoke(null,null);
Thread.Sleep(0); //Allow Delegates to get started

//Create WaitHandleArray
WaitHandle[] myWaiters = new WaitHandle[2];
myWaiters[0] = ar.AsyncWaitHandle;
myWaiters[1] = ar2.AsyncWaitHandle;

if (!WaitHandle.WaitAll(myWaiters,1000,false))
{
//Timed Out
Debug.WriteLine("Threads Timed Out");
}
else
{
Debug.WriteLine("Threads Completed");
dlgt.EndInvoke(ar);
dlgt2.EndInvoke(ar2);
}
}
}
}

Thanks
Bill


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

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