Solved

How to kill a thread immediatley

Posted on 2004-08-26
8
178 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
WPF issue with Trigger 2 69
Data Saving 5 40
Get String split 5 33
what technologies offer Authentication over Web Services? 4 71
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now