RE: Unable to Abort a listening thread

From: Dan Kelley (DanKelley_at_discussions.microsoft.com)
Date: 01/20/05


Date: Thu, 20 Jan 2005 01:31:07 -0800

Check out this site: http://www.yoda.arachsys.com/csharp/threads/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
>



Relevant Pages

  • 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: 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() ... i'm trying to stop the thread if the app is closing: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Some Problem With FileSystemWatcher openNETCF
    ... void File_Monitor_Deleted(object sender, FileSystemEventArgs ... I want after i start my app and browse for example to the My Documents ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: WM_COPY,WM_PASTER override in CEdit
    ... The problem has to do with the overrides to WM_COPY and WM_PASTE in my ... I have 2 edit controls in a dialog based app. ... afx_msg void OnPaste; ...
    (microsoft.public.vc.mfc)
  • Re: Anyway to handle Today event...?
    ... BringToFront() doesn't work if user has clicked "TODAY" menu.... ... void btnCreateBForm_Click{ ... When you run this app, ... > parent form is visible upon the return from ShowDialog ...
    (microsoft.public.dotnet.framework.compactframework)