Solved

How to kill a thread immediatley

Posted on 2004-08-26
8
181 Views
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.]
0
Comment
Question by:jjacksn
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 22

Expert Comment

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

0
 
LVL 37

Assisted Solution

by:gregoryyoung
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.
0
 
LVL 5

Author Comment

by:jjacksn
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
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 37

Expert Comment

by:gregoryyoung
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 ?
0
 
LVL 5

Author Comment

by:jjacksn
ID: 11907373
totally different apps.  
0
 
LVL 37

Expert Comment

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

Accepted Solution

by:
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.
0
 
LVL 19

Assisted Solution

by:drichards
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:

            try
            {
                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
            }
0

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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