How should I use Thread.Interrupt (general question)?

  • Thread starter Thread starter TT \(Tom Tempelaere\)
  • Start date Start date
T

TT \(Tom Tempelaere\)

Hi people,

This is a question on the use of Thread.Interrupt. I would love to hear from
anyone who ever used the Interrupt method on a thread to unblock it.

My C# book sais that Interrupt method will interrupt a wait, sleep or join
performed on the thread.

Now suppose that I'm coding a DB app using C#, and that the data queries run
on a sperate thread. The queries take a long time to execute, and I need a
way to unblock the thread. Would Thread.Interrupt unblock the thread, i.e.
will the underlying implementation of ADO.NET use WaitHandle's? (I assume
ADO.NET is the way to code DB apps.)

Perhaps that is a bad example because it depends on the implementation of
ADO.NET, and I don't even know ADO.NET yet. My intent with Thread.Interrupt
is to use it in my (base) thread class as a general way to unblock it. But I
have my doubts if I can use it for that in all cases.

I'm thinking of not using Thread.Interrupt in my base class, and allow the
derived class to do the Thread.Interrupt when it deems necessary. Seems like
safest and most flexible. Comments?

Thanks,
PS: The book also mentions that I should avoid relying on Thread.Interrupt
to unblock a thread. It mentions problems with unmanaged code executed via
interop (will not unlock), and the fact that it won't unblock
Thread.SpinWait. Any other good reasons why I shouldn't?
PS2: A different Question: does ADO.NET provide an explicit way to cancel a
long running query (or call, ...)?
 
Did you not post this question in another news group? It reads familiar.

The thread interrupt will interrupt only on a blocking call. For the
unmanaged world, it cannot touch the thread until it returns so the call is
pretty much ignored. Calling interrupt requires that you catch the
interruptexception otherwise the operating system will kill the thread
forcibly.
way to unblock the thread. Would Thread.Interrupt unblock the thread,
yes

I'm not sure what the recommended pattern here is. Maybe someone more
knowledgable can interject. I believe calling interrupt is very intrusive
and ought to be avoided when possible. (I'm not sure where I got this from
so it may not be correct).
 
Back
Top