|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Monitoring ThreadPool threads?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,...) >> This is where I want to monitor the thread from the previous line of code. 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 Show quote "JS" <gq_s2***@hotmail.com> wrote in message news:1135203248.897690.38100@g43g2000cwa.googlegroups.com... > 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,...) > >>> This is where I want to monitor the thread from the previous line of >>> code. > > Thanx in advance. > > J. > > The thread that monitors the handle registered in My mistake...I should have been more clear in my original post. I want> RegisterWaitForSingleObject will call a callback, specified in the 2nd > parameter. > So what do you want to monitor? 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: I would expect that I would want to check if the thread is still> - wait completed - callback will be called > - still wating - no callback 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? Show quote "JS" <gq_s2***@hotmail.com> wrote in message news:1135250485.260143.183730@g43g2000cwa.googlegroups.com... >> 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. > Vadym Stetsyak wrote:
Show quote > You can use event to tell your parent thread that work is done... Yes, you can, by why would you want to? I mean, isn't this the> > 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 > } > 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... Show quote "Damien" <Damien_The_Unbelie***@hotmail.com> wrote in message news:1135251943.576663.262540@g47g2000cwa.googlegroups.com... > Vadym Stetsyak wrote: >> 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 > 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 ) Show quote "JS" <gq_s2***@hotmail.com> wrote in message news:1135254253.625685.74190@g47g2000cwa.googlegroups.com... > 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. > |
|||||||||||||||||||||||