473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 12130
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Jane Austine | last post by:
As far as I know python's threading module models after Java's. However, I can't find something equivalent to Java's interrupt and isInterrupted methods, along with InterruptedException....
2
by: Marco | last post by:
Hallo, I have made a microcontroller with keyboard (16 key). I have a function to handle interrupt that comes from keyboard when a key is pressed. I should make a C software (not graphic) with...
2
by: anil | last post by:
Hi Friends!! Does any body know the interrupt no. of the CD-Rom device to make it function like opening and reading etc..If any body knoow tell me ..I want to learn hardware programming through...
0
by: LordHog | last post by:
Hello all, I have a little application that will be used for test environment that utilizes the CAN Messaging. The application will capture and then decode the CAN message that are received. In...
2
by: bvermeersch | last post by:
Hi, Here I am again, just learning C# and always wanting to learn the hard things first. As you might already know from other posts I'm trying to convert c++ code to C#. The problem I'm...
2
by: Hartmut | last post by:
Hi guys out there..., I am not shure if this is the right forum for my Question. Have a problem using my own Keyboard interrupt handler. The handler works fine as long as i do not press keys...
0
by: John Bailo | last post by:
Does c# have a method for threads equivalent to the .interrupt() method? I want to implement a watchdog thread and have it throw an error onces the timeout value is reached. then I want it to...
2
by: =?Utf-8?B?QnJ1Y2UgSFM=?= | last post by:
I'm using VS2005 Winforms. I want a method to allow the user to be able to interrupt an SQL query if he decides he doesn't want to wait for it any longer. I've used IAsyncResult to open an...
1
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
I have a problem where I am sending a series of commands using P/Invoke's SendInput function which may last up to a minute long. I want to offer the user a way to break the commands by pressing the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.