none
How to kill a server thread RRS feed

  • Question

  •      I have an application that create a server in a new thread. This thread is an infinite loop witch wait for the client to connect to it.
        
        How can I kill the server thread on form close event ?

       
    I had tried   next solution but it doesn't work.

          [System.Runtime.InteropServices.DllImport("kernel32.dll")]
            static extern IntPtr OpenThread(uint dwDesiredAccess, bool bInheritHandle, uint dwThreadId);

            [System.Runtime.InteropServices.DllImport("kernel32.dll")]
            static extern bool TerminateThread(IntPtr hThread, uint dwExitCode);


          ProcessThreadCollection processThreads =  Process.GetCurrentProcess().Threads;
          foreach (ProcessThread pt in processThreads)
            {
                IntPtr ptrThread = OpenThread(1, false, (uint)pt.Id);
                TerminateThread(ptrThread, 1);
            }


    thanks.
      
    Monday, January 5, 2009 11:47 PM

Answers

  • You want to set IsBackground = true on your server thread if you want it to shutdown automatically.

           -Steve
    • Marked as answer by Naineol Tuesday, January 6, 2009 9:18 PM
    Tuesday, January 6, 2009 8:48 PM

All replies

  •  What do you mean does not work? If it does, a thread cannot be terminated immediately. What is your criterion for thread termination? Do you check it in a Thread Browser window or you check it programmatically?

    By the same token, why do you want to terminate a thread on Form1_Close event? Close the form and the GC will take care of all your threads. Your posts leaves too many questions unanswered. You should be more specific as to what you want, why and what the problem is. To say "it does not work" is to say nothing. Do you get an exception?

    You can terminate a thread with Abort command, although again, it will not happen immediately.


    AlexB

    Tuesday, January 6, 2009 1:07 AM
  • I use next function to create a server
     
     

    public void server()
            {

                try
                {
                    IPAddress ipAd = IPAddress.Parse("127.0.0.1");
                    TcpListener myList = new TcpListener(ipAd, 8001);
                    while (true)
                    {
                        myList.Start();
                        Socket s = myList.AcceptSocket();
                        byte[] b = new byte[100];

                        int k = s.Receive(b);
                        string message = "";
                        for (int i = 0; i < k; i++) message += Convert.ToChar(b[i]);
                        MessageBox.Show(message);
                        
                        ASCIIEncoding asen = new ASCIIEncoding();
                        s.Send(asen.GetBytes("I receive your message"));

                        s.Close();
                                           
                    }

                    myList.Stop();
                }
                catch
                {
                    MessageBox.Show("The server can't be create");
                }
               
            }


    , and    next code to create the thread


    ThreadContainer objContainer = new ThreadContainer(some parametars); 
                            Thread objThread = new Thread(new ThreadStart(objContainer.server));
                            objThread.Start();


     and the problem is that anytime I open the program ( supposed his name is Server.exe  ) and I create a new server after I close it in the Windows Task Manager in the Processes tab remained a process named Server.exe , and if I repeat this operation will be 2 process and so on.

      I think the problem appears  because the TcpListner will never be close. but I don't know how to solve it.


    If you have any sugestion please help

    Thanks.

     

    Tuesday, January 6, 2009 5:49 PM
  • You want to set IsBackground = true on your server thread if you want it to shutdown automatically.

           -Steve
    • Marked as answer by Naineol Tuesday, January 6, 2009 9:18 PM
    Tuesday, January 6, 2009 8:48 PM
  •  


        Now  it works .

        Thanks
    Tuesday, January 6, 2009 9:19 PM