473,385 Members | 1,531 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.

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 2236
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
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...
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...

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.