Monitoring ThreadPool threads?

  • Thread starter Thread starter JS
  • Start date Start date
J

JS

All,

I have an application that has asynchronous HTTPWebRequest calls
incorporated into it. While I am waiting for a handler that monitors
the connection for a timeout I want to monitor the thread itself in the
ThreadPool that I have started. By "monitor" I am implying I just want
to track its execution status (if it is complete or not).

Can it be done?

Code snippet:

....<some code>...

ThreadPool.RegisterWaitForSingleObject(arg1,arg2,...)

Thanx in advance.

J.
 
The thread that monitors the handle registered in
RegisterWaitForSingleObject will call a callback, specified in the 2nd
parameter.
So what do you want to monitor?

there are 2 states of the "monitor" thread:
- wait completed - callback will be called
- still wating - no callback
 
The thread that monitors the handle registered in
RegisterWaitForSingleObject will call a callback, specified in the 2nd
parameter.
So what do you want to monitor?

My mistake...I should have been more clear in my original post. I want
the parent thread to wait until the thread in the ThreadPool is done
executing (i.e. wait until the callback has executed).
there are 2 states of the "monitor" thread:
- wait completed - callback will be called
- still wating - no callback

I would expect that I would want to check if the thread is still
waiting.

Just to add to my original post....
I have started to consider a custom threadpool for what I am doing
rather than to use the built-in ThreadPool class. Would that be better
than trying to monitor the ThreadPool?

Thanks in advance for the response.

J.
 
You can use event to tell your parent thread that work is done...

void ParentThread()
{
ThreadPool.QueueUserWorkItem(worker...);
myEvent.WaitOne(); //here we wait for
//our worker thread
}

void WorkerThread(object obj)
{
//do the work
myEvent.Set(); //notify parent thread
}

Why do you want to monitor ThreadPool?
 
Vadym said:
You can use event to tell your parent thread that work is done...

void ParentThread()
{
ThreadPool.QueueUserWorkItem(worker...);
myEvent.WaitOne(); //here we wait for
//our worker thread
}

void WorkerThread(object obj)
{
//do the work
myEvent.Set(); //notify parent thread
}
Yes, you can, by why would you want to? I mean, isn't this the
equivalent of:

void ParentThread()
{
WorkerThread(obj);
}

(except it saves ever mucking about with the thread pool in the first
place)

Damien
 
That was an example how to do this when threadpool is used. ( execution in
parallel )

The OP didn't mention why he desires to monitor threads...
 
Hey Vadym/Damien,

Yes, I probably have not been clear enough in this post. This is my
first time working with threading and my vocabulary is not where it
should be (I suspect).

When using the ThreadPool my desire is once I have called the following
line of code (from OP):

ThreadPool.RegisterWaitForSingleObject(arg1,arg2,...)

Is that the main thread will wait until the callback has been executed.


Does that make sense?

J.
 
Maybe in particualr circumstances.

RegisterWaitForSingleObject doesn't block that is you main thread will be
still executing. RegisterWaitForSingleObject spawns new thread to wait for
the object specified

But if you only want to wait on the object maybe it will be simpler to call

arg1.WaitOne();
instead of
RegisterWaitForSingleObject ?

You will achieve the desired behaviour:
Main thread will wait until the desired object is signaled.

Now about RegisterWaitForSingleObject . In what scenarios it can be used.
main thread needs some data to send to network Data is retrieved by another
thread that has waitobject and when data is retrieved it signales that
object. While this is happening main thread is not waiting but doing smth
else ( perfoms caluclations )
 
Back
Top