The thread is not sleeping. It's just hanged in the Socket.Receive method.
It's status is running...
Main Topics
Browse All TopicsI want to abort (kill) a thread no matter what. In the documentation it says that if the thread is in an unmanaged code portion when the ThreadAbortException is thrown, the system will rethrow it when managed code is executed. But what if the thread is stuck in an unmanaged method? how can I kill the thread?
(as a side note, Socket.Receive() appears to be unmanaged!!! )
here is a small sample that uses 2 threads that get hung in a socket.receive and cannot be aborted. (i wanted them to get hunged!). If the socket part is commented, it works!
using System;
using System.Threading;
using System.Net;
using System.Net.Sockets;
namespace ConsoleApplication2
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///
class MyThread
{
public MyThread()
{
}
public void run()
{
int i=0;
int k=0;
Console.WriteLine("init thread " + this.GetHashCode());
while(true)
{
Console.WriteLine("thread " + this.GetHashCode() + " " + i++);
Thread.Sleep(1000);
//----begin socket part---
Socket sock = new Socket(AddressFamily.Inter
//bogus address, nothigs come from here so that the Receive method will run forever!
IPAddress hostIPAddress1 = (Dns.Resolve("europe.battl
byte[] bytes = new byte[256];
k=sock.Receive(bytes);
Console.WriteLine("Receive
//---end socket part---
}
}
}
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Start();
}
public static void Start()
{
MyThread myTh1 = new MyThread();
Thread th1 = new Thread(new ThreadStart(myTh1.run));
th1.Name="TH1";
th1.Start();
MyThread myTh2 = new MyThread();
Thread th2 = new Thread(new ThreadStart(myTh2.run));
th2.Name="TH2";
th2.Start();
Thread.Sleep(5000);
th1.Abort();
Thread.Sleep(500);
Console.WriteLine("th1 aborted -> status: " + th1.ThreadState.ToString()
Thread.Sleep(6000);
th2.Abort();
Thread.Sleep(500);
Console.WriteLine("th2 aborted -> status: " + th2.ThreadState.ToString()
}
}
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
One last thing....
Are you catching the ThreadAbortException ? It doesn't look like it in this code (but this may not be ALL of your code). This is a bit trivial, but good programming practice. If you are going to kill a thread unexpectedly (for any reason), always have a catch routine to close down all connections and dispose of all other objects gracefully.
public void run()
{
int i=0;
int k=0;
try
{
while(true)
{
...
...
...
}
}
catch(ThreadAbortException
{
sock.Shutdown();
}
}
SeanChapman -> the socket does not throw an exception if there is nothing to receive!! It just hangs in the Receive method forever.
_TAD_ -> The ThreadAbortException is not thrown while the execution is hanged inside the receive method!!
if no other brilliant idea comes along, i'll accept the IsBackground answer, because that one at least prevents the program's zombification if a thread cannot be aborted.
Business Accounts
Answer for Membership
by: _TAD_Posted on 2003-09-02 at 07:22:47ID: 9267450
I don't think you can abort a thread that is "sleeping". I think you need to check the status first
if (th1.ThreadState == ThreadState.WaitSleepJoin)
{
th1.Interrupt();
th1.Abourt();
}