How to kill a thread immediatley

Posted on 2004-08-26
Last Modified: 2010-04-15
I am trying to kill a thread immediately by calling Thread.Abort().  However, the thread is not catching the ThreadAbortException until after a very long Remote Method call.  I want to be able to not wait for this call to return in certain circumstances.  Is there a way to do this?  

[there is a case where a second application opens and will use the xml database that the first application is writing to after the remote method returns.  the first application runs in the background all the time and I don't mind that it will have to send this method call again.  I MUST have the UI for the second applcation appear immediately though, and the UI accesses the XML file and thus I need to tell the first application to stop writing to it or I will have concurrency problems.]
Question by:jjacksn
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
LVL 22

Expert Comment

ID: 11906799
Call the API routine
BOOL TerminateThread( HANDLE hThread, DWORD dwExitCode);

LVL 37

Assisted Solution

gregoryyoung earned 100 total points
ID: 11906860
jjacksn there are other ways of doing this ... I posted one in your other thread on this (async processing). Another option would be to use shared file access which would avoid your concurrency problem since the second application is only reading and the first application is only writing.

Author Comment

ID: 11907251
gregory, it is already doing async processing.  I don't see another good way of doing this, but i'm totally open to one.  

Let me try explaining my problem more clearly.  

Application A is the main app that has a UI and needs to be very fast to load and responsive

Application B is a tray app that does stuff in the background and occasionaly uploads a lot of data to the server.  This data is not critical and just needs to get on the server at some point.

both A and B read AND write to a file Database.xml.  

If a user opens A when B is in the middle of its update method, we have a problem, since A could then make a change, B could make a change, and one of the changes would get lost.  Thus, as soon as A opens, B needs to be killed.  A sends a message to B saying I'm open, now, I need B to kill its thread that is working with Database.xml.  

Futhermore, B does have a small popup that says "uploading changes" "upload cancelled" and "upload completed".  

When I can't think of a good way (other than using a bunch of flags) than the thread method i described.  

cookre, do you know why abort is working immediately
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 37

Expert Comment

ID: 11907286
look in the other question jjacksn also these threads are in the same application ? why not use locking to avoid the issue of them both accessing the file concurrently ?

Author Comment

ID: 11907373
totally different apps.  
LVL 37

Expert Comment

ID: 11907387
why not use a mutex to synchronize (or another native lock)
LVL 22

Accepted Solution

cookre earned 250 total points
ID: 11908059
Thread.Abort() raises an exception to be processed by the target thread.  If the target thread is  hung or busy elsewhere, it can take a palpable abount of time to get around to handling the exception,  The API call is a draconian measure performed by the kernal that requires no participation of the target thread.

Be aware that TerminateThread() is the court of last resort that may leave in-use resources in an indeterminate state.
LVL 19

Assisted Solution

drichards earned 150 total points
ID: 11908183
Depending on how your app works, you might be able to do something as simple as using the file itself as a synchronization point.  If you open the file and use None as the file share mode, then when the second app tries to open it it will get an exception that the file is in use.  The downside of this is you now need to poll to check when the file becomes available.  For Application A that "needs to be very fast to load and responsive", this is probably what you want anyway.  For App B, if you already have some sort of loop, then this will probably suffice.  Otherwise, App B can just exit the thread if it gets a sharing violation rather than your proposal above which is to have some outside agent kill the thread.

If you're using XmlTextReader, you can do something like this:

                System.IO.FileStream fstream = System.IO.File.Open("..\\..\\XMLFile1.xml", System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None);
                rdr = new XmlTextReader(fstream);  // rdr is XmlTextReader from outside this code
            catch ( System.Exception ex)
                // Examine exception to see if it's sharing violation

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How come this regular expression allows "e"? 14 58
Problem to Office 1 44
Securing WEBAPI on Azure 2 26
aspx ascx, c# 7 14
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question