473,387 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Unable to Abort a listening thread

HI,

As i posted below i have an app with a separate thread listening for a tcp client connection.
In the simplest way it looks like:

void ListenerThreadFunction()
{
TcpListener l = new TcpListener(IpAddress.Parse("127.0.0.1"), 8080);
l.Start();
Socket client = l.AcceptSocket();
client.close;
l.Stop();
}

This function is invoked from Form1_Load event of the main form:

Thread MyThread = null;

void Form1_Load(...)
{
...
MyThread = new Thread(new ThreadStarter(ListenerThreadFunction));
MyThread.Start();
}

Now, i'm trying to stop the thread if the app is closing:

void Form1_Closing(...)
{
...
if ((MyThread!=null) && MyThread.isAlive)
MyThread.Abort();
}

But it doesn't work - though main form is closed, i still see that the app proccess is alive in IDE.
MSDN says that Abort throws an exception to the thread, but it never gets there...

All of described happens of course if server didn't get a connection and the thread is alive.

I'm not that good with threads and think that i misuse the concept somehow...
Any suggestions would be highly appreciated!!!

Thank you in advance!
Andrey
Nov 16 '05 #1
2 3955
MuZZy wrote:
HI,

As i posted below i have an app with a separate thread listening for a
tcp client connection.
In the simplest way it looks like:

void ListenerThreadFunction()
{
TcpListener l = new TcpListener(IpAddress.Parse("127.0.0.1"), 8080);
l.Start();
Socket client = l.AcceptSocket();
client.close;
l.Stop();
}

This function is invoked from Form1_Load event of the main form:

Thread MyThread = null;

void Form1_Load(...)
{
...
MyThread = new Thread(new ThreadStarter(ListenerThreadFunction));
MyThread.Start();
}

Now, i'm trying to stop the thread if the app is closing:

void Form1_Closing(...)
{
...
if ((MyThread!=null) && MyThread.isAlive)
MyThread.Abort();
}

But it doesn't work - though main form is closed, i still see that the
app proccess is alive in IDE.
MSDN says that Abort throws an exception to the thread, but it never
gets there...

All of described happens of course if server didn't get a connection and
the thread is alive.

I'm not that good with threads and think that i misuse the concept
somehow...
Any suggestions would be highly appreciated!!!


I could use Thread.isBackground = true, this way the thread closes when the main app closes,
but i need to be able to close the thread "gracefully" - i have a log file open in that listener thread and i need to close the file before
terminating the thread.
Nov 16 '05 #2
Check out this site: http://www.yoda.arachsys.com/csharp/...shutdown.shtml

There are some excellent articles in there on threading best practices.

HTH
Dan

"MuZZy" wrote:
HI,

As i posted below i have an app with a separate thread listening for a tcp client connection.
In the simplest way it looks like:

void ListenerThreadFunction()
{
TcpListener l = new TcpListener(IpAddress.Parse("127.0.0.1"), 8080);
l.Start();
Socket client = l.AcceptSocket();
client.close;
l.Stop();
}

This function is invoked from Form1_Load event of the main form:

Thread MyThread = null;

void Form1_Load(...)
{
...
MyThread = new Thread(new ThreadStarter(ListenerThreadFunction));
MyThread.Start();
}

Now, i'm trying to stop the thread if the app is closing:

void Form1_Closing(...)
{
...
if ((MyThread!=null) && MyThread.isAlive)
MyThread.Abort();
}

But it doesn't work - though main form is closed, i still see that the app proccess is alive in IDE.
MSDN says that Abort throws an exception to the thread, but it never gets there...

All of described happens of course if server didn't get a connection and the thread is alive.

I'm not that good with threads and think that i misuse the concept somehow...
Any suggestions would be highly appreciated!!!

Thank you in advance!
Andrey

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Drew Stoddard | last post by:
We are working in C#/Winforms and are using asynchronous client-side web service calls (the Begin... and End... methods supplied by the web reference creation). Many of these calls are contained...
7
by: Morris | last post by:
I want to abort a running thread, so I call MyThread.abort() function. My problem is this thread runs "almost" like a while(true) loop and I don't want the Abort() function interrupts the thread at...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
12
by: MuZZy | last post by:
Hi, Sorry for a repeated post but i didn't receive an answer and will try to re-phrase my question: How do i close an additional thread from the main thread, if this additional thread is stuck...
18
by: Urs Vogel | last post by:
Hi I wrote an application server (a remoting sinlgeton), where processes must be stopped in very rare cases, done thru a Thread.Abort(). Occasionally, and only after a Thread.Abort(), this...
6
by: Joe HM | last post by:
Hello - I have a function that calls Thread.Abort() to stop a thread in a _Closed() Method of a GUI. The thread contains a blocking call on a TCP socket and that is the easiest way to stop...
1
by: Terrance | last post by:
I'm trying to create a small messenger program that uses the tcpclient and tcplistenter objects. When I start the application and run the thread that fires the tcplistener; once the client sends...
6
by: mehdi | last post by:
Hi folks, You know, the Thread class has got a method named Abort which according to the msdn: "Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of...
2
by: tbh | last post by:
in an error path in an aspx script under DotNet 2, IIS6, Win2003 Server I get the following error (which I don't understand) on Respone.End(): {Unable to evaluate expression because the code is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.