Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Thread.Abort() doesn't kill it

Posted on 2009-05-07
6
Medium Priority
?
682 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 300 total points
ID: 24324191
{
  expThread.Abort();
  expThread.Join();
}
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

719 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