ThreadPool - Terminating Function Exsists?

N

NvrBst

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.


Thanks.
NB.
 
B

Ben Voigt

NvrBst said:
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);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top