473,406 Members | 2,387 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,406 software developers and data experts.

How to close a thread

Hi.
In my program, I'm initiating a thread in (console application) by getting a signal from my client(windowsform)who hits a connect button and it works the way i want.....
Now i am trying to close this thread when my client clicks on the disconnect button but i'm unable to find a way.

Kindly suggest what should be done to close the thread and terminate the form.

Here is the code I tried

Expand|Select|Wrap|Line Numbers
  1. //declaring a flag to keep a check on when the user hits disconnect
  2.  
  3. public int flag = 0;
  4.  
  5. //to start a thread  
  6.  
  7. private void button2_Click(object sender, EventArgs e)
  8.         {
  9.             readData = "Connected to Chat Server ...";
  10.             msg();
  11.             clientSocket.Connect("192.168.1.7", 5060);
  12.             serverStream = clientSocket.GetStream();
  13.  
  14.             byte[] outStream = System.Text.Encoding.ASCII.GetBytes(textBox2.Text + "$" + textBox4.Text + "%");
  15.             serverStream.Write(outStream, 0, outStream.Length);
  16.             serverStream.Flush();
  17.  
  18.             Thread ctThread = new Thread(getMessage);
  19.             ctThread.Start();
  20.        if (flag == 10)
  21. {
  22. ctThread.Abort();
  23. }
  24.         }
  25.  
  26. // other processes
  27.  
  28. // When the client hits diconnect
  29.  
  30.   private void button3_Click_1(object sender, EventArgs e)
  31.         {
  32.             readData = "You have left the session";
  33.             msg();
  34.  
  35.          // here i want to close the respective thread
  36.  
  37. flag = 10;
  38.         }
  39.  
  40.  
Aug 7 '10 #1

✓ answered by Christian Binder

The simplest way would be to declare ctThread as a private variable, assign it in button2_Click() and call it's Abort()-method in button3_Click_1()

e.g.
Expand|Select|Wrap|Line Numbers
  1. class xy {
  2.   Thread ctThread = null;
  3.  
  4.   private void button2_Click(object sender, EventArgs e) {
  5.    // ...
  6.    ctThread = new Thread(getMessage);
  7.    ctThread.Start();
  8.    // ...
  9.   }
  10.  
  11. private void button3_Click_1(object sender, EventArgs e) {
  12.    // ...
  13.    if(ctThread != null)
  14.      ctThread.Abort();
  15.    // ...
  16.   }
  17. }
  18.  

2 8884
Christian Binder
218 Expert 100+
The simplest way would be to declare ctThread as a private variable, assign it in button2_Click() and call it's Abort()-method in button3_Click_1()

e.g.
Expand|Select|Wrap|Line Numbers
  1. class xy {
  2.   Thread ctThread = null;
  3.  
  4.   private void button2_Click(object sender, EventArgs e) {
  5.    // ...
  6.    ctThread = new Thread(getMessage);
  7.    ctThread.Start();
  8.    // ...
  9.   }
  10.  
  11. private void button3_Click_1(object sender, EventArgs e) {
  12.    // ...
  13.    if(ctThread != null)
  14.      ctThread.Abort();
  15.    // ...
  16.   }
  17. }
  18.  
Aug 9 '10 #2
Thank you very much. It worked for me.
Aug 9 '10 #3

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

Similar topics

3
by: James Dunkerley | last post by:
Hi, I am trying to create a class which downloads a web page in the background and then raises an event in the original thread in which it was created. This class is going to be the basis of a...
1
by: Ebrahim | last post by:
This message is in reply to a prev 1 . My application refues to close . Some one had suggested that I might have threads running.. but i solved that problem too . The app still refuses to close...
13
by: Jason Jacob | last post by:
To all, I have a GUI program (use c#), and I have create a Thread for loading some bulk data, I also arrange the GUI program like this: 1) load a form showing "Wait for loading..." etc 2) a...
20
by: Doug Thews | last post by:
I ran into an interesting re-pain delay after calling the Abort() method on a thread, but it only happens the very first time I call it. Every time afterward, there is no delay. I've got a...
16
by: droopytoon | last post by:
Hi, I start a new thread (previous one was "thread timing") because I have isolated my problem. It has nothing to do with calling unmanaged C++ code (I removed it in a test application). I...
12
by: MuZZy | last post by:
Hi, Sorry for a repeated post but i didn't receive an answer and will try to re-phrase my question: How do i close an additional thread from the main thread, if this additional thread is stuck...
3
by: Robert Sorger | last post by:
I have a problem with thread execution after calling response.redirect() in a web form. Basically, I want the thread to continue to work (e.g. do some long database work) but want to redirect...
4
by: Robin Tucker | last post by:
Some thread confusion to lighten up your day: I have a worker thread and a main form. When the user clicks the Close icon in the top right of the form, my program tells the thread to "stop". ...
2
by: Miro | last post by:
VB 2003. I cant find the last thing im missing. I click the "Run" button to run my app in VB.net and it runs. But when i close the application, the thread does not end ( i think ) because it...
1
by: raghudr | last post by:
Hi all, I am displaying a splash screen for which i have created a thread.Since my whole project is launched by windows service and that service will start automatically at the start of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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,...

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.