How to interrupt a method ?

  • Thread starter Thread starter Const
  • Start date Start date
C

Const

Hello,

I wonder how i could interrupt the execution of a method.

Like :

My method can work on some lerg text file (severals Mb), and i would
like to interrupt it by pressing "escape" tuch for exemaple.

How ??

Thank you! :)

Const.
 
Const,

You would need to run this method on another thread, most likely. What
you would do is check a flag (a boolean) on each iteration of your loop (as
you process lines from the file). As you do this, if the flag gets set to
true, then you stop the processing of the loop (just return from your
method).

Then, on your UI thread, you would set the flag to true if the escape
button was pressed.

You also need to lock access to the field, with a lock statement.

Hope this helps.
 
Hi,
Instead of checking for a flag (every iteration) you could
probably call the Thread.Abort from the UI Thread, which would abort
the worker thread (thread that is reading the file). You dont have lock
the variable which might affect your perf.

Naveen
 
Naveen,

Calling Thread.Abort is a very BAD BAD BAD BAD (did I say BAD) idea!

It is not recomended practice, and it sure as hell isn't clean. You can
really tank the state of an application by doing that.
 
| Hi,
| Instead of checking for a flag (every iteration) you could
| probably call the Thread.Abort from the UI Thread, which would abort
| the worker thread (thread that is reading the file). You dont have lock
| the variable which might affect your perf.
|
| Naveen
|

This is a bad suggestion, you should never call Thread.Abort from user code.

Willy.
 
Other wrote:
"This is a bad suggestion, you should never call Thread.Abort from user
code."
"Calling Thread.Abort is a very BAD BAD BAD BAD (did I say BAD) idea!"

I disagree with these statements. Calling Thread.Abort() is perfectly
acceptable. Let me explain my point of view.

An application with one thread handling the UI and a different thread,
the worker thread, executing some lengthy operation is a perfectly
acceptable solution. That way the UI stays responsive. There are
obviously other cases where starting and terminating threads is the
best (and sometimes the only) design choice, e.g. how do you cancel a
blocking call? Terminating the process?

Polling a flag is certainly not an option. It is not deterministic in
particular if the code in the thread does not poll the flag, e.g. when
a blocking call is made. (How do you time out when you try to get
exclusive access to a file?)

There is, however, a suggested coding technique for properly shutting
down a thread when it is being terminated using Thread.Abort(). The
runtime throws a ThreadAbortException which should be handled in the
thread. See documentation at
http://msdn2.microsoft.com/en-us/library/cyayh29d.aspx.

Best regards,
Manfred.
 
See inline.

| Other wrote:
| "This is a bad suggestion, you should never call Thread.Abort from user
| code."
| "Calling Thread.Abort is a very BAD BAD BAD BAD (did I say BAD) idea!"
|
| I disagree with these statements.

No problem, allow me to disagree with yours ;-)

Calling Thread.Abort() is perfectly
| acceptable. Let me explain my point of view.
|

Only if you call it on your own thread, or if you are going to unload the AD
anyway. The situation in V2 is not that bad, as some changes have been made
to the CLR to give you a chance to recover from asynchronous thread aborts,
but it's still hard to get it right.



| An application with one thread handling the UI and a different thread,
| the worker thread, executing some lengthy operation is a perfectly
| acceptable solution. That way the UI stays responsive. There are
| obviously other cases where starting and terminating threads is the
| best (and sometimes the only) design choice, e.g. how do you cancel a
| blocking call? Terminating the process?
|
Thread.Abort cannot cancel a blocking call, it can only terminate a thread
that is executing JIT compiled code, a thread that executes or blocks in
unmanaged code cannot be terminated, the CLR has no control over threads
that are transitioning into unmanaged code.

| Polling a flag is certainly not an option. It is not deterministic
So is Thread.Abort see the docs.
<The thread is not guaranteed to abort immediately, or at all...>

in
| particular if the code in the thread does not poll the flag, e.g. when
| a blocking call is made. (How do you time out when you try to get
| exclusive access to a file?)

What do you mean by that? A thread that can open a file exclusively won't
block when performing IO, even if it could block waiting for IO completion,
it's blocked in unmanaged code, so Thread.Abort won't succeed either. A
program that tries to open a file exclusively will throw if it can't open
the file.


|
| There is, however, a suggested coding technique for properly shutting
| down a thread when it is being terminated using Thread.Abort(). The
| runtime throws a ThreadAbortException which should be handled in the
| thread. See documentation at
| http://msdn2.microsoft.com/en-us/library/cyayh29d.aspx.
|

All I can say is that this part of the docs is misleading at best!

Please read these to understand why:
http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation
http://weblogs.asp.net/justin_rogers/archive/2004/02/02/66537.aspx
or from MSFT folks on the CLR team...

http://blogs.msdn.com/cbrumme/archive/2003/04/17/51361.aspx
http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=c1898a31-a0aa-40af-871c-7847d98f1641


Willy.
 
Back
Top