473,387 Members | 1,548 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,387 software developers and data experts.

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 ThreadAbortException is thrown

Thanks

Jeff
Jul 21 '05 #1
4 2237
Jeff,
I have not tried it, however I understand that you can catch the
ThreadAbortException then call Thread.ResetAbort to cancel the Abort
request.

Of course you will need to design your routine to be "restartable" 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*******@discussions.microsoft.com> wrote in message
news:5B**********************************@microsof t.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 ThreadAbortException is thrown?
Thanks,

Jeff

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

Of course you will need to design your routine to be "restartable" 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*******@discussions.microsoft.com> wrote in message
news:5B**********************************@microsof t.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 ThreadAbortException 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 ThreadAbortException 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*******@discussions.microsoft.com> wrote in message
news:5B**********************************@microsof t.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 ThreadAbortException 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 ThreadAbortException 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*******@discussions.microsoft.com> wrote in message
news:5B**********************************@microsof t.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 ThreadAbortException 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
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 (...
7
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...
20
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...
18
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...
1
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...
2
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...
6
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...
23
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() {...
5
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 =...
6
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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.