Closing thread and program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • coolx
    New Member
    • Jan 2010
    • 5

    Closing thread and program

    Hi.
    I am using thread function in my project. When I close program.exe(for m), it is still working at background and in task manager, i can see program.exe still working.

    How can I close this program completely?
    -----------------------------------------------------------

    Code:
    public void Form1_Load(object sender, EventArgs e)
            {
                CheckForIllegalCrossThreadCalls = false;
                Thread thread_dinleyici = new Thread(new ThreadStart(dinle));
                thread_dinleyici.Start();
            }
    
            public void dinle()
            {
                //....
                while(true)
                {
                    istemcisoketi = tcp_listener.AcceptSocket();
                    if (istemcisoketi.Connected)
                    {
                      // ...
                        while (rdr_gonderilecek.Read())
                        {
                            gonderilecek_data += rdr_gonderilecek["iletiler"].ToString();
                        }
                        TcpClient tcp_client = new TcpClient(karsi_ip, 30001);
                        ag_akimi = tcp_client.GetStream();
                        akim_yazici = new StreamWriter(ag_akimi);
                        akim_yazici.WriteLine(gonderilecek_data);
                        akim_yazici.Flush();
                    }
                }
            }
    Last edited by tlhintoq; Jan 3 '10, 09:28 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Because you created the thread inside the Load method, you have no way to reference from other methods. Move the creation outside of any methods to give it Form-Wide scope, so you can reach it from all methods within the form.

      Code:
      Thread thread_dinleyici = new Thread(new ThreadStart(dinle));
      
      public void Form1_Load(object sender, EventArgs e)
              {
                  CheckForIllegalCrossThreadCalls = false;
                  thread_dinleyici.Start();
                  // ...
      Now in your Form_Closing event handler you can abort the thread.
      Code:
      public void form1_Closing(object sender, EventArgs e)
      {
            thread_dinleyici.Abort();
      }

      Comment

      • coolx
        New Member
        • Jan 2010
        • 5

        #4
        Hi,
        Firstly thanks for your reply tlhintoq.

        I used your codes but I am getting another error;
        "A field initializer cannot reference the non-static field, method, or property 'socket_sunucu. Form1.dinle()'"


        Code:
        [B] Thread thread_dinleyici = new Thread(new ThreadStart(dinle));
        [/B]
                public Form1()
                {
                    InitializeComponent();
                }
        
                public void Form1_Load(object sender, EventArgs e)
                {
                    CheckForIllegalCrossThreadCalls = false;
                    thread_dinleyici.Start();
                }
        
                public void dinle()
                {
                   TcpListener tcp_listener = new TcpListener(30000);
                   tcp_listener.Start();
                    while(true)
                    {
                        
                        istemcisoketi = tcp_listener.AcceptSocket();
                        if (istemcisoketi.Connected)
                        {
                             ...
                            while (rdr_gonderilecek.Read())
                            {
                                gonderilecek_data += rdr_gonderilecek["iletiler"].ToString();
                            }
                            TcpClient tcp_client = new TcpClient(karsi_ip, 30001);
                            ag_akimi = tcp_client.GetStream();
                            akim_yazici = new StreamWriter(ag_akimi);
                            akim_yazici.WriteLine(gonderilecek_data);
                            akim_yazici.Flush();
                        }
                    
                    }

        Comment

        • coolx
          New Member
          • Jan 2010
          • 5

          #5
          Hi,
          I used like this, its getting no error but unfortunately the program still working at background.
          Code:
           Thread thread_dinleyici;
          ...
          ...
                  
           public void Form1_Closing(object sender, FormClosingEventArgs e)
                  {
                      thread_dinleyici.Abort();
                  }

          Comment

          • coolx
            New Member
            • Jan 2010
            • 5

            #6
            Finally I wrote the line above and its working but is it any side affect for program?
            Code:
            Environment.Exit(0);

            Comment

            • piyusht
              New Member
              • Jan 2010
              • 8

              #7
              Using Thread.Abort() is not recommended because it can cause crash or data loss sometimes. The recommended approach is to keep a flag for the thread and reset it when the form is closing. I've always used this approach its trial and tested.

              Here is modified code:

              Code:
              #       [B] private bool _shutdown = false;[/B]
              #         public Form1()
              #         {
              #             InitializeComponent();
              #         }
              #      
              #         public void Form1_Load(object sender, EventArgs e)
              #         {
              #             CheckForIllegalCrossThreadCalls = false;
              #             thread_dinleyici.Start();
              #         }
              #  
              #         public void dinle()
              #         {
              #            TcpListener tcp_listener = new TcpListener(30000);
              #            tcp_listener.Start();
              #            [B] while(_shutdown==false)[/B]
              #             {
              #  
              #                 istemcisoketi = tcp_listener.AcceptSocket();
              #                 if (istemcisoketi.Connected)
              #                 {
              #                      ...
              #                     while (rdr_gonderilecek.Read())
              #                     {
              #                         gonderilecek_data += rdr_gonderilecek["iletiler"].ToString();
              #                     }
              #                     TcpClient tcp_client = new TcpClient(karsi_ip, 30001);
              #                     ag_akimi = tcp_client.GetStream();
              #                     akim_yazici = new StreamWriter(ag_akimi);
              #                     akim_yazici.WriteLine(gonderilecek_data);
              #                     akim_yazici.Flush();
              #                 }
              #  
              #             }
              
                      private void Form1_FormClosing(object sender, FormClosingEventArgs e)
                      {
                              [B] _shutdown = true;[/B]
                      }
              In this way the thread's operation gets complete and then the form closes gracefully. I hope this helps.

              Comment

              Working...