"NvrBst" <qu****@hotmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
>I have a thread pool. Is there a function I can override that all the
ThreadPool work queues call before they terminate? IE so I can just
do something like
override void _threadpoolterminate(...) {
//My 1 line of code "Running[this] = false;"
base(...);
}
Kinda thing...
I have about 50 functions I call with the ThreadPool, and each time
they return I have to do "Running[ThreadPoolItem] = false; return;".
And most the functions return at multiple spots... its gets a little
annoying/messy expecially if I forget to set running to false before a
return.
Something like this:
class ThreadPoolHelper
{
public static Queue<ObjectType>(ObjectType that, string methodName, object
state)
{
ThreadPool.QueueUserWorkItem(new PoolShim<ObjectType>(obj,
typeof(ObjectType).GetMethod(methodName)).DoIt, state);
}
private class PoolShim<ObjectType>
{
delegate void WorkDelegate(ObjectType that, object state);
readonly ObjectType that;
readonly WorkDelegate action;
public PoolShim(ObjectType that, MethodInfo m)
{
this.that = that;
this.action = (WorkDelegate)Delegate.Create(typeof(WorkDelegate) ,
m);
}
public void DoIt(object state)
{
try {
action(that, state); // binds delegate at call time, like C++
(that->*action)(state)
}
finally {
Running[that] = false;
}
}
}
}
now, instead of
ThreadPool.QueueUserWorkItem(obj.method, state);
use
ThreadPoolHelper.Queue(obj, "method", state);
>
Thanks.
NB.