Terminating active threads in Multi-threaded application question

G

Guest

Hi,

I'm working on a multi-threaded application. I'm using ThreadPools.

One of the requirements of the application is, if certain exception occurs
all the
counters have to be reset. I have noticed that some threads, even after
resetting
the counters, have the old values.

Is there any way that we can disable/abort/kill all the active threads when
certain exception occurs.

How can we make sure that the all the counters are synchronised in all
threads.
As I have to port this code to Windows CE platform later on, I guess
aborting
threads may not be one option.

please let me know your suggestions for this scenario.

Cheers,

Naveen.
 
M

Moty Michaely

Hey Naveen

What kine of Thread synchronization module are you using (if any).

Synchronizing threads is not so trivial. Try using Mutex, Monitor class,
lock etc. to synchronize your objects in a multi-threaded environment (Check
MSDN).

You can use ManualResetEvent and AutoResetEvent to synchronize events
between threads to cancel threaded operation in your threadStart delegate.

private void ThreadedMethod
{
while (true)
{
// Wait for cancelation Event for 100 ms. This event should be set
in other // thread
if [AutoResetEvent variable].WaitOne(100)
{
//Cancel the method, reset counters or what so ever
}
else
{
//Continue your method..
}
}
}

Hope this helps.

- Moty -
 

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