471,319 Members | 1,625 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

How to interrupt a method ?

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.
Jan 13 '06 #1
6 11904
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Const" <ne**@ampdesign.net> wrote in message
news:43**********************@nan-newsreader-05.noos.net...
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.

Jan 13 '06 #2
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

Jan 13 '06 #3
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Naveen" <na*********************@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
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

Jan 13 '06 #4

"Naveen" <na*********************@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
| 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.
Jan 13 '06 #5
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.
---
Manfred Lange
http://www.manfred-lange.com
http://manfredlange.blogspot.com
ml at agileutilities dot com

Jan 13 '06 #6
See inline.

"Manfred" <ml@agileutilities.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
| 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/iangblo...2/cancellation
http://weblogs.asp.net/justin_rogers.../02/66537.aspx
or from MSFT folks on the CLR team...

http://blogs.msdn.com/cbrumme/archiv.../17/51361.aspx
http://www.bluebytesoftware.com/blog...c-7847d98f1641
Willy.
Jan 13 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Jane Austine | last post: by
2 posts views Thread by Marco | last post: by
2 posts views Thread by anil | last post: by
2 posts views Thread by bvermeersch | last post: by
2 posts views Thread by Hartmut | last post: by
reply views Thread by John Bailo | last post: by
2 posts views Thread by =?Utf-8?B?QnJ1Y2UgSFM=?= | last post: by
1 post views Thread by =?Utf-8?B?TXJOb2JvZHk=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.