Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to kill a thread immediatley

Posted on 2004-08-26
8
Medium Priority
?
186 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
[X]
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
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 400 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 1000 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 600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

636 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