Solved

Thread.Abort() doesn't kill it

Posted on 2009-05-07
6
680 Views
Last Modified: 2012-05-06
My abort button doesn't work. My code is something like this.

try {
   if (expThread != null) {
           if (expThread.IsAlive) {
              expThread.Abort();
          }
   }
}
catch (Exception Ex)
{
                MessageBox.Show("Error aborting scan: " + Ex.Message);
}


Im developing a Windows application (not Web based) using Visual Studio 2008 and Visual C#

When I press my ABORT button I trace through the code and it always executes expThread.Abort(); but expThread.IsAlive is always true. Shouldn't it be set to false when Abort() is executed?  

An exception is never encountered. ie. The MessageBox is not displayed.

Not sure what is happenning here. Anyone have any ideas?
0
Comment
Question by:rangers99
[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
6 Comments
 
LVL 12

Expert Comment

by:andrewjb
ID: 24324116
It;'s asyncronous - use Thread.Join() to wait for the .Abort() to be processsed.

Your thread can also ignore the abort request.
0
 

Author Comment

by:rangers99
ID: 24324135
andrew
Thanks. Where does Thread.Join() go in the code?
0
 
LVL 12

Accepted Solution

by:
andrewjb earned 100 total points
ID: 24324191
{
  expThread.Abort();
  expThread.Join();
}
0
Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

 
LVL 6

Expert Comment

by:Cebik
ID: 24324194
and it's better to use

catch (ThreadAbortException)
{
      return;
}

Open in new window

0
 

Author Comment

by:rangers99
ID: 24324794
Cebik , yes its better but its not really the issue here.

andrew
I tried thread.Join() but my application just hangs. My thread appears to be un-abortable. Im not sure what the hell is going on.
0
 
LVL 12

Expert Comment

by:andrewjb
ID: 24324841
What's your thread doing? .Abort() raises a ThreadAbortException on the thread. If the thread catches+ignores that, it'll never stop. Or if it is itself stuck waiting for a signal or mutex, then it'll remain waiting.
I can't tell what the issue is without seeing what the thread is doing.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

617 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