473,799 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Thread.Abort()

I am running a worker thread that manipulates some hardware setting every so often. My problem is that the hardware manipulation cannot be interrupted once it has started

How can I ensure that the entire operation will run to completion even when the ThreadAbortExce ption is thrown

Thanks

Jeff
Jul 21 '05 #1
4 2257
Jeff,
I have not tried it, however I understand that you can catch the
ThreadAbortExce ption then call Thread.ResetAbo rt to cancel the Abort
request.

Of course you will need to design your routine to be "restartabl e" at the
proper point of the hardware setting. VB.NET allows the use of Goto in the
catch block to a label in the Try block, which may be useful in this
regard...

Hope this helps
Jay

"Jeff" <an*******@disc ussions.microso ft.com> wrote in message
news:5B******** *************** ***********@mic rosoft.com...
I am running a worker thread that manipulates some hardware setting every so often. My problem is that the hardware manipulation cannot be
interrupted once it has started.
How can I ensure that the entire operation will run to completion even when the ThreadAbortExce ption is thrown?
Thanks,

Jeff

Jul 21 '05 #2
Jeff,
I have not tried it, however I understand that you can catch the
ThreadAbortExce ption then call Thread.ResetAbo rt to cancel the Abort
request.

Of course you will need to design your routine to be "restartabl e" at the
proper point of the hardware setting. VB.NET allows the use of Goto in the
catch block to a label in the Try block, which may be useful in this
regard...

Hope this helps
Jay

"Jeff" <an*******@disc ussions.microso ft.com> wrote in message
news:5B******** *************** ***********@mic rosoft.com...
I am running a worker thread that manipulates some hardware setting every so often. My problem is that the hardware manipulation cannot be
interrupted once it has started.
How can I ensure that the entire operation will run to completion even when the ThreadAbortExce ption is thrown?
Thanks,

Jeff

Jul 21 '05 #3
The only way you can be absolutely, positively sure the operation runs to
completion is to write it using unmanaged code - the runtime will not
interrupt unmanaged code except when shutting down the entire runtime, and
even then only after a watchdog timeout. If you are accessing hardware you
may already be using unmanaged code - without more description of how you
manipulate the hardware it is impossible to tell. And even then if some
piece of code calls Process.Kill() you can be terminated. There are no
absolute guarantees.

The ThreadAbortExce ption can be thrown manually, and it is also injected
into a thread when an appdomain gets unloaded. This exception will interrupt
threads executing managed code, even in a finally block. There are a few
things you can do to minimize your exposure in managed code. First, run this
code in a separate thread that you create yourself (or use a threadpool
thread) and do not expose the thread object you are using. This prevents
other code from manually calling Thread.Abort on that thread since it will
not have a reference to it. This will protect you against an arbitrary
client aborting the thread, but it still leaves you exposed to an appdomain
getting unloaded.
It may be that you don't really need to run to completion. Any hardware
access with tight timing requirements should be done in a device driver, not
in managed code. Perhaps if you describe what you want in more detail
someone here can be of more help.
"Jeff" <an*******@disc ussions.microso ft.com> wrote in message
news:5B******** *************** ***********@mic rosoft.com...
I am running a worker thread that manipulates some hardware setting every so often. My problem is that the hardware manipulation cannot be
interrupted once it has started.
How can I ensure that the entire operation will run to completion even when the ThreadAbortExce ption is thrown?
Thanks,

Jeff

Jul 21 '05 #4
The only way you can be absolutely, positively sure the operation runs to
completion is to write it using unmanaged code - the runtime will not
interrupt unmanaged code except when shutting down the entire runtime, and
even then only after a watchdog timeout. If you are accessing hardware you
may already be using unmanaged code - without more description of how you
manipulate the hardware it is impossible to tell. And even then if some
piece of code calls Process.Kill() you can be terminated. There are no
absolute guarantees.

The ThreadAbortExce ption can be thrown manually, and it is also injected
into a thread when an appdomain gets unloaded. This exception will interrupt
threads executing managed code, even in a finally block. There are a few
things you can do to minimize your exposure in managed code. First, run this
code in a separate thread that you create yourself (or use a threadpool
thread) and do not expose the thread object you are using. This prevents
other code from manually calling Thread.Abort on that thread since it will
not have a reference to it. This will protect you against an arbitrary
client aborting the thread, but it still leaves you exposed to an appdomain
getting unloaded.
It may be that you don't really need to run to completion. Any hardware
access with tight timing requirements should be done in a device driver, not
in managed code. Perhaps if you describe what you want in more detail
someone here can be of more help.
"Jeff" <an*******@disc ussions.microso ft.com> wrote in message
news:5B******** *************** ***********@mic rosoft.com...
I am running a worker thread that manipulates some hardware setting every so often. My problem is that the hardware manipulation cannot be
interrupted once it has started.
How can I ensure that the entire operation will run to completion even when the ThreadAbortExce ption is thrown?
Thanks,

Jeff

Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
5363
by: Daisy | last post by:
From this page: http://www.c-sharpcorner.com/2/mt_beginner1.asp Thread class's Abort method is called to kill a thread permanently. Make sure you call IsAlive before Abort. if ( thread.IsAlive ) { thread.Abort(); }
7
3293
by: Morris | last post by:
I want to abort a running thread, so I call MyThread.abort() function. My problem is this thread runs "almost" like a while(true) loop and I don't want the Abort() function interrupts the thread at any point in the thread. In fact, I have a section of code needs to be "protected" from being interrupted. How can I make sure Abort() will not land anywhere winthin this block? In other words, the Abort() must wait until this block of code is done...
20
3036
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a delegate inside the UI that I call to update the progress meter. I use the Suspend() and Abort() methods based on button events. I can watch the progress meter increase just fine when the thread is running. When I select Start, I enable the Cancel...
18
5862
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this component becomes instabile, throwing a Windows like error (access violation on 0x00000002), not an framework exception. The component and all of its subcomponents are 100% managed code. What could go wrong with Thread.Abort()? Thanks for any hints.
1
4475
by: benmorganpowell | last post by:
I have a small windows service which connects to a POP3 server at defined intervals, scans the available messages, extracts the required information and inserts the data into a SQL database. I am assuming that this is not an uncommon piece of software. I want to get an architecture that conforms as closely as possible with the recommendations from Microsoft on developing Windows Services, but to be honest I have found difficultly in...
2
4289
by: Mark Denardo | last post by:
I'm trying to abort a suspended thread, but I get a ThreadStateException: An unhandled exception of type 'System.Threading.ThreadStateException' occurred in mscorlib.dll Additional information: Thread is suspended; attempting to abort. I have a number or threads in my program - some running, some suspended. I hope I don't have to start the thread up again just to abort it??
6
5483
by: Joe HM | last post by:
Hello - I have a function that calls Thread.Abort() to stop a thread in a _Closed() Method of a GUI. The thread contains a blocking call on a TCP socket and that is the easiest way to stop that. This thread is also outputting strings in a RichTextBox and in some rare instances I get a System.NullReferenceException when I exit the GUI. It seems like the _Closed() Method calls Thread.Abort() and then continues closing down/disposing...
23
5719
by: Boltar | last post by:
Hi I'm writing a threading class using posix threads on unix with each thread being run by an object instance. One thing I'm not sure about is , if I do the following: myclass::~myclass() { : : do stuff
5
5082
by: andrew | last post by:
Hi, I have the following issue with the Thread.Abort(): The main thread creates a worker thread which waits on a process termination. void ThreadProc() { Process proc = proc.Start("notepad.exe");
6
2945
by: mehdi | last post by:
Hi folks, You know, the Thread class has got a method named Abort which according to the msdn: "Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread." I've had a long discussion with someone on not to use the mentioned method unless under the most extreme cases. I believe that it's
0
9685
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
10470
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...
1
10214
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
9067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5459
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...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
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
3
2935
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.