Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

thread.abort() not working

Status
Not open for further replies.

djones7936

Programmer
Jul 20, 2002
24
0
0
US
Hello,

I've got a run away thread!

I am making a very simple program which accepts connections on post 80 (HTTP), sends an HTTP header and a short webpage, then closes the connection. (Essentially, a very simple web server.) I have a TcpListener in a separate class which is accessed using a thread. I'm using a thread because normally, the user interface freezes until there is a request for a webpage. :)
While the program "listens", the code in the thread stays at the tcpListener.AcceptSocket() line, and waits for a webpage request.

The program appears to run okay, and the webpage displays properly, but the thread still runs after the close button is pressed. I used the thread.Abort(); command, but the thread seems to keep going as if the command was never executed.

I put thread.Abort(); in the Dispose subroutine of my program. When the program closes, the thread should abort/stop, but it doesn't.

Is thread.Abort() supposed to stop then destroy the thread? Has anyone experienced this problem, or know what might cause it?

Thanks for any input!
-Dave

 
Are you using the declared name of the thread to abort? Ie

Thread m_Thread;
ThreadStart m_ThreadStart = new ThreadStart(myMethod);
m_Thread = new Thread(m_ThreadStart);
m_Thread.Abort();

Something along those lines worked just fine for my program. Also make sure that the thread is declared in such a way that it is accesible from wherever you're trying to abort it.

SlantyOD
 
I tried your code, and the code works, (I added in m_Thread.Start(); and that works too :)). But when I include the tcplistr.AcceptSocket(); line of code, the thread keeps running after the program stops :-(.

I experimented with your code. I declared the Thread m_Thread; and ThreadStart m_ThreadStart; at the top of the class "Form1" (so all the methods can access it). I stored "myMethod" in a separate class called "Class1." myMethod consisted of an endless while loop. At Form1_Load, I used the code:
m_ThreadStart = new ThreadStart(Class1.myMethod);
m_Thread = new Thread(m_ThreadStart);
m_Thread.Start();

In Form1.Dispose I put:
m_Thread.Abort();

When I start the program the thread starts, when I stop the program, the thread aborts like normal. The code works as it is suppose to.

If I change the code in Class1.myMethod() to:
TcpListener tcplistr = new TcpListener(80);
tcplistr.Start();
Socket socketForClient = tcplistr.AcceptSocket();
When I run the program, the thread does not stop when the program is "closed". It seems the thread is stuck on that line of code and isn't unloading as I would like. This problem does not occur when I comment out the tcplistr.AcceptSocket(); line, though I need that code for the program to work.

Maybe I should use a different method instead of tcplistr.AcceptSocket() or use a different approach than using TcpListener?

-Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top