Threading problem

M

mfc

I have code like so :-

try {
while (true) {
ProcessInQueue();

try {
Thread.Sleep(Timeout.Infinite);
} catch (ThreadInterruptedException) {
}
}
} catch (ThreadAbortException) {
}

but when i interrupt the thread, i get the ThreadInterruptedException thrown
in the ProcessInQueue(). There are no sleep or wait statements in
ProcessInQueue() that i have put in, ProcessInQueue() does call api
functions that may be making the thread sleep or wait. How can i get around
this problem? I don't want ProcessInQueue() interrupted.
 
D

David Browne

mfc said:
I have code like so :-

try {
while (true) {
ProcessInQueue();

try {
Thread.Sleep(Timeout.Infinite);
} catch (ThreadInterruptedException) {
}
}
} catch (ThreadAbortException) {
}

but when i interrupt the thread, i get the ThreadInterruptedException
thrown in the ProcessInQueue(). There are no sleep or wait statements in
ProcessInQueue() that i have put in, ProcessInQueue() does call api
functions that may be making the thread sleep or wait. How can i get
around this problem? I don't want ProcessInQueue() interrupted.

Don't abort or interupt your threads. It appears that you are sleeping your
thread, and aborting it to wake up. You just can't do that. If you want
your thread to sleep, have it wait on a ManualResetEvent. Then from other
code you can signal the event. Or have it do a Monitor.Wait, and signal it
with a Monitor.Pulse.




David
 
D

David Browne

mfc said:
solved... seems calling Control.Invoke sends the thread into a wait state.
So avoid Control.Invoke like the plague...

No. Fix your code. Sleeping the current thread while the window thread
does some work for you is what Contorl.Invoke is for. And it's the only way
to marshal changes back to the UI from a background thread.

David
 
M

mfc

solved... seems calling Control.Invoke sends the thread into a wait state.
So avoid Control.Invoke like the plague...
 
M

mfc

thanks... works a treat! :)

David Browne said:
No. Fix your code. Sleeping the current thread while the window thread
does some work for you is what Contorl.Invoke is for. And it's the only
way to marshal changes back to the UI from a background thread.

David
 

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