473,408 Members | 1,772 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,408 software developers and data experts.

Closing thread and program

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

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

Expand|Select|Wrap|Line Numbers
  1. public void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             CheckForIllegalCrossThreadCalls = false;
  4.             Thread thread_dinleyici = new Thread(new ThreadStart(dinle));
  5.             thread_dinleyici.Start();
  6.         }
  7.  
  8.         public void dinle()
  9.         {
  10.             //....
  11.             while(true)
  12.             {
  13.                 istemcisoketi = tcp_listener.AcceptSocket();
  14.                 if (istemcisoketi.Connected)
  15.                 {
  16.                   // ...
  17.                     while (rdr_gonderilecek.Read())
  18.                     {
  19.                         gonderilecek_data += rdr_gonderilecek["iletiler"].ToString();
  20.                     }
  21.                     TcpClient tcp_client = new TcpClient(karsi_ip, 30001);
  22.                     ag_akimi = tcp_client.GetStream();
  23.                     akim_yazici = new StreamWriter(ag_akimi);
  24.                     akim_yazici.WriteLine(gonderilecek_data);
  25.                     akim_yazici.Flush();
  26.                 }
  27.             }
  28.         }
Jan 3 '10 #1

✓ answered by tlhintoq

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.

Expand|Select|Wrap|Line Numbers
  1. Thread thread_dinleyici = new Thread(new ThreadStart(dinle));
  2.  
  3. public void Form1_Load(object sender, EventArgs e)
  4.         {
  5.             CheckForIllegalCrossThreadCalls = false;
  6.             thread_dinleyici.Start();
  7.             // ...
  8.  
Now in your Form_Closing event handler you can abort the thread.
Expand|Select|Wrap|Line Numbers
  1. public void form1_Closing(object sender, EventArgs e)
  2. {
  3.       thread_dinleyici.Abort();
  4. }
  5.  

6 2983
tlhintoq
3,525 Expert 2GB
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.
Jan 3 '10 #2
tlhintoq
3,525 Expert 2GB
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.

Expand|Select|Wrap|Line Numbers
  1. Thread thread_dinleyici = new Thread(new ThreadStart(dinle));
  2.  
  3. public void Form1_Load(object sender, EventArgs e)
  4.         {
  5.             CheckForIllegalCrossThreadCalls = false;
  6.             thread_dinleyici.Start();
  7.             // ...
  8.  
Now in your Form_Closing event handler you can abort the thread.
Expand|Select|Wrap|Line Numbers
  1. public void form1_Closing(object sender, EventArgs e)
  2. {
  3.       thread_dinleyici.Abort();
  4. }
  5.  
Jan 3 '10 #3
coolx
5
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()'"


Expand|Select|Wrap|Line Numbers
  1.  Thread thread_dinleyici = new Thread(new ThreadStart(dinle));
  2.         public Form1()
  3.         {
  4.             InitializeComponent();
  5.         }
  6.  
  7.         public void Form1_Load(object sender, EventArgs e)
  8.         {
  9.             CheckForIllegalCrossThreadCalls = false;
  10.             thread_dinleyici.Start();
  11.         }
  12.  
  13.         public void dinle()
  14.         {
  15.            TcpListener tcp_listener = new TcpListener(30000);
  16.            tcp_listener.Start();
  17.             while(true)
  18.             {
  19.  
  20.                 istemcisoketi = tcp_listener.AcceptSocket();
  21.                 if (istemcisoketi.Connected)
  22.                 {
  23.                      ...
  24.                     while (rdr_gonderilecek.Read())
  25.                     {
  26.                         gonderilecek_data += rdr_gonderilecek["iletiler"].ToString();
  27.                     }
  28.                     TcpClient tcp_client = new TcpClient(karsi_ip, 30001);
  29.                     ag_akimi = tcp_client.GetStream();
  30.                     akim_yazici = new StreamWriter(ag_akimi);
  31.                     akim_yazici.WriteLine(gonderilecek_data);
  32.                     akim_yazici.Flush();
  33.                 }
  34.  
  35.             }
Jan 4 '10 #4
coolx
5
Hi,
I used like this, its getting no error but unfortunately the program still working at background.
Expand|Select|Wrap|Line Numbers
  1.  Thread thread_dinleyici;
  2. ...
  3. ...
  4.  
  5.  public void Form1_Closing(object sender, FormClosingEventArgs e)
  6.         {
  7.             thread_dinleyici.Abort();
  8.         }
  9.  
Jan 4 '10 #5
coolx
5
Finally I wrote the line above and its working but is it any side affect for program?
Expand|Select|Wrap|Line Numbers
  1. Environment.Exit(0);
  2.  
Jan 4 '10 #6
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:

Expand|Select|Wrap|Line Numbers
  1. #        private bool _shutdown = false;
  2. #         public Form1()
  3. #         {
  4. #             InitializeComponent();
  5. #         }
  6. #      
  7. #         public void Form1_Load(object sender, EventArgs e)
  8. #         {
  9. #             CheckForIllegalCrossThreadCalls = false;
  10. #             thread_dinleyici.Start();
  11. #         }
  12. #  
  13. #         public void dinle()
  14. #         {
  15. #            TcpListener tcp_listener = new TcpListener(30000);
  16. #            tcp_listener.Start();
  17. #             while(_shutdown==false)
  18. #             {
  19. #  
  20. #                 istemcisoketi = tcp_listener.AcceptSocket();
  21. #                 if (istemcisoketi.Connected)
  22. #                 {
  23. #                      ...
  24. #                     while (rdr_gonderilecek.Read())
  25. #                     {
  26. #                         gonderilecek_data += rdr_gonderilecek["iletiler"].ToString();
  27. #                     }
  28. #                     TcpClient tcp_client = new TcpClient(karsi_ip, 30001);
  29. #                     ag_akimi = tcp_client.GetStream();
  30. #                     akim_yazici = new StreamWriter(ag_akimi);
  31. #                     akim_yazici.WriteLine(gonderilecek_data);
  32. #                     akim_yazici.Flush();
  33. #                 }
  34. #  
  35. #             }
  36.  
  37.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  38.         {
  39.                  _shutdown = true;
  40.         }
  41.  
In this way the thread's operation gets complete and then the form closes gracefully. I hope this helps.
Jan 5 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: simon place | last post by:
Spent some very frustrating hours recoding to find a way of closing a server socket, i'd not thought it would be any problem, however, after complete failure and as a last resort, i looked at the...
6
by: Gary Miller | last post by:
Does anyone know how to detect a modeless form on closing by the form that invoked the modeless form? form.Show();
22
by: Zytan | last post by:
I have public methods in a form. The main form calls them, to update that form's display. This form is like a real-time view of data that is changing. But, the form may not exist (it is...
4
by: indianteam | last post by:
I am trying to write a multi-threaded program and use pexpect along with it. All works fine until I try to close the connection handle. That thread keeps waiting until the connection handle closes....
0
by: Vishal Sethia | last post by:
I am trying to write a multi-threaded program and use pexpect along with it. All works fine until I try to close the connection handle. That thread keeps waiting until the connection handle closes....
1
by: sewid | last post by:
Hi there! I've got a problem with no solution, I hope you might help me. I am writing a small tool with many buttons. Every button starts a thread and this thread starts something else, in the...
12
by: kronecker | last post by:
I found this nifty routine that closes a form one by one every time it is called. However, I need to hide them instead of closing them. Is there a way to alter the code? I assume it has something...
2
by: =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?= | last post by:
I am (still) relatively new to Windows applications, most of my experience has been Web based, and I am confused about what exactly happens when the Main() method is called and how to manipulate...
4
Fr33dan
by: Fr33dan | last post by:
Hi, I'm having trouble with a multi-threaded program crashing on a specific machine when the worker thread is not initialized at when the _FormClosing method of my main form called. Here is my...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.