Re: Unable to Abort a listening thread

From: MuZZy (leyandrew_at_yahoo.com)
Date: 01/20/05


Date: Thu, 20 Jan 2005 01:15:30 -0500

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.



Relevant Pages

  • RE: Unable to Abort a listening thread
    ... > As i posted below i have an app with a separate thread listening for a tcp client connection. ... > void ListenerThreadFunction() ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Socket security question
    ... allows local processes owned by the same user ... who owns the (the process that created the listening) socket ... a user runs this app and this app ... I assume if the socket is listening to 127.0.0.1 ...
    (microsoft.public.win32.programmer.networks)
  • Re: Kerio and .NET Applications
    ... >> So is it possible to make it ask before an app opens a listening ... > I don't think there's anything you can do to prevent an app or program ... But it's kind of funny how PFW solutions with App Control ... Keep in mind that .Net is a Standard like JAVA is a Standard and MS ...
    (comp.security.firewalls)
  • Re: Cable broadband, wired router and iLamp
    ... cause a prompt the first time they try listening to the network, ... You'll only get asked again if the app changes and the app's author ... No matter what I did with the application firewall settings the problem always reappeared. ...
    (uk.comp.sys.mac)
  • Re: How to do Async TCP Listener?
    ... the listner from my app, but the app is locked up while it's listening. ... If so, your client ... >> Public Sub DoAcceptTcpClientCallback ...
    (microsoft.public.dotnet.languages.vb)