Thread start and Finished Notification

  • Thread starter Thread starter Kiran
  • Start date Start date
K

Kiran

Hi,

Was wondering if its possible to have a event notification in Thread when
the thread is about to start and the thread has just finished doing its work
and about to terminate.
If its not directly available, I was thinking to derive a class form Thread
and provide this events, but again its a sealed class. Is there a way to do
this?
I do not want to waste time pooling or waiting for the other thread to
finish I can rather so someting useful.

Any help is most welcome.

Regards
Kiran
 
Hi Kiran,

Just declare the events in your main code, then when the thread starts it
can trigger the event asynchronously using BeginInovke method of your event.
Same when the thread finishes the job.

hope this helps

Fitim Skenderi
 
Hi,

Will this kind of helped, Now I created my own class that wraps this
Thread class & the Execute method.

Thanks Again
Kiran
 
Hi Kiran

----- Kiran wrote: ----
Was wondering if its possible to have a event notification in Thread whe
the thread is about to start and the thread has just finished doing its wor
and about to terminate

Yes, but not automatically. I see two ways to achieve this.
(1) Pass two callbacks to your thread, one for signaling the thread has started, the other to signal that the thread has finished. In your thread, call the callbacks at the appropriate moment (start & end). Beware of synchronization issues. The callbacks are wrapped using delegates
(2) Provide two events for your thread, one that signals thread startup, the other signals thread completion. Signal these events at the appropriate moment from your thread. Have another thread monitor these events. The possible event classes that I am talking about are ManualResetEvent and AutoResetEvent (and Mutex, but not useful in this context). These classes derive from WaitHandle, and provide wait functionality

If its not directly available, I was thinking to derive a class form Threa
and provide this events, but again its a sealed class. Is there a way to d
this

The Thread class is sealed, so aggregation/composition is your instrument. I think it is common to write a base thread class for your projects which does all plumbing

I do not want to waste time pooling or waiting for the other thread t
finish I can rather so someting useful

Then I suggest you use callbacks. These could post a message, which you could process when you have the time. This is easily done using a queue.

Regard
Kira

HTH
Tom Tempelaere.
 
Back
Top