Solved

How to kill a thread immediatley

Posted on 2004-08-26
8
180 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 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