473,666 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12157
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.co m

"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.co m

"Naveen" <na************ *********@gmail .com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.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******** *************@g 49g2000cwa.goog legroups.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 ThreadAbortExce ption 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@agileutilit ies.com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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 ThreadAbortExce ption 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
6472
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. "somethread.interrupt()" will wake somethread up when it's in sleeping/waiting state. Is there any way of doing this with python's thread? I suppose thread interrupt is a very primitive functionality for stopping a blocked thread.
2
2446
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 a menu. I thought to make into main.c a cicle like this: while (1)
2
1905
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 C.. awaiting reply ----anil
0
1759
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 the API reference material the device will generate an interrupt which the user much provide an interrupt service routine with the entry pointed at the interrupt 0x16 vector ( Address = 0x0083 ). Is it possible to write an interrupt service...
2
7063
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 running into now is the creation and handling of an interrupt. In c++ it is done this way:
2
4087
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 like pause, arrow keys etc. My system returns with a stack overflow. Is there something i have overseen in my keyboard Interrupt Service
0
1206
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 throw an exception based on the thread interrupt. -- Get the new http://www.you-read-it-here-first.com
2
3269
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 async connection that runs an ExecuteNonQuery, but this method doesn't allow interruption (EndExecuteNonQuery gets blocked until the execute finishes), and ideally I'd like to interrupt a Query rather than a NonQuery. It seems to me this is a...
1
2510
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 Pause/Break button. The problem I have is it seems that my SendInput commands are being quickly accepted and added to a buffer, because the only time my program ever processes the Pause/Break key press event is after my SendInput commands are...
0
8440
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8355
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8866
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8550
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.