Solved

Closing a multithreaded application

Posted on 2006-11-09
4
351 Views
Last Modified: 2012-06-27
does any one know how i can close a multithreaded application ?

I cannot check for a boolean type, because each thread will stay waiting on the accepttcpclient() command of the socket object.

I have a list of thread that i started in my main program, can i destroy each thread in the destructor ?? and if yes, how ??
0
Comment
Question by:JoepKillaars
4 Comments
 
LVL 1

Expert Comment

by:qing02051981
ID: 17912726
In C#

For example if you socket listening codes are like this

bool flag=true;

while(flag){
     //you listening codes here
}

You can close the socket so each thread does not stay at the socket.receivedata using socket.Close()

After that you can set the flag to false and kill the threads using abort

flag = false;
myThread.Abort();
0
 

Author Comment

by:JoepKillaars
ID: 17912739
I know that it can be done like that.

But in the thread there is waited for a connection by the socket.accepttcpclient() method, so i cannot create a while loop checking a boolean, I have to somehow force the accepttcpclient to stop listening and destroy the thread, without checking a boolean in a while loop.

Any one has suggestions ?
0
 
LVL 2

Expert Comment

by:basavaraj_kn
ID: 17912744
can you place your code here

Or simply place wait at the end of your programm so that your program waits till all thread finishes

Cheers..!
BAS
0
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 125 total points
ID: 17913610
You can set the IsBackground property (of the Thread object) to true before starting the thread, background threads dont prevent the application from closing and when the application is closed the CLR will stop any running background threads automatically:

http://msdn2.microsoft.com/en-gb/library/system.threading.thread.isbackground.aspx
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question