Click here to Skip to main content
15,900,818 members
Home / Discussions / C#
   

C#

 
GeneralRe: switch statemeent. Am I missing someting Pin
Mark Churchill19-Jun-08 18:53
Mark Churchill19-Jun-08 18:53 
QuestionTele-type screen saver Pin
Elvis_Pretzelator19-Jun-08 9:32
Elvis_Pretzelator19-Jun-08 9:32 
AnswerRe: Tele-type screen saver Pin
Spacix One19-Jun-08 9:42
Spacix One19-Jun-08 9:42 
GeneralRe: Tele-type screen saver Pin
Elvis_Pretzelator19-Jun-08 9:47
Elvis_Pretzelator19-Jun-08 9:47 
GeneralRe: Tele-type screen saver Pin
Spacix One19-Jun-08 10:19
Spacix One19-Jun-08 10:19 
GeneralRe: Tele-type screen saver Pin
Elvis_Pretzelator25-Jun-08 10:02
Elvis_Pretzelator25-Jun-08 10:02 
GeneralRe: Tele-type screen saver Pin
Elvis_Pretzelator26-Jun-08 5:39
Elvis_Pretzelator26-Jun-08 5:39 
QuestionC# Thread termination issue Pin
Merlock219-Jun-08 8:39
Merlock219-Jun-08 8:39 
Hey everyone,

I have recently built a client and server application for transferring files across the network. The client application uses two threads: one for transferring file data to the server and another for monitoring incoming messages from the server. Both threads use separate NetworkStream objects for their respewctive tasks.
The job of the listener thread is to listen for messages from the server (which are in the from of strings). Now, when the server disconnects for whatever reason, it'll send a "Disconnect" string message to the client. The listener thread will receive that message, interpret it, and then tell the client application to disconnect from the server.

to illustrate what I mean, here is some code from my client:

//the thread monitoring incoming messages from the server
private Thread m_oListenerThread;

//the thread in charge of file transfer
private Thread m_oTransferThread;

//when 'true', the listener thread exits its loop and terminates
private volatile bool m_bListenerStop;

//when 'true', the transfer thread exits its loop and terminates
private volatile bool m_bTransferThreadStop;

//here is the method run by the listener thread:
StartListening()
{
//some initialization details here
//...
while(m_bListenerStop)
{
//check the NetworkStream object for any data
//...

//if data available, extract string from NetworkStream
//...

//parse message (where sMessage is the extracted string message)
HandleCommand(sMessage);

}
//thread termination details
//...
}

HandleCommand(string z_sMessage)
{
if (z_sMessage == "Disconnect")
{
DisconnectFromServer();
}
}

This is simple enough, but in order to make the application more robust, I need to ensure that all threads have terminated. The only way I know of so far is to call Join(nMiliseconds) on the thread which you want to test. This will cause the calling thread (ex: main) to wait for the thread until nMiliseconds has expired, or the thread is no longer active. Therefore, the DisconnectFromServer() method would look something like this:

DisconnectFromServer()
{
//signal threads to terminate
m_bListenerStop = true;
m_bTransferThreadStop = true;

//close all connection elements
//...

//ensure all threads have stopped
if (m_oListenerThread != null)
{
UpdateStatusDisplay("Waiting for listener thread to terminate...");

//wait for 302 milliseconds for listener thread to terminate. If the thread has not terminated
//by that time, abort thread and throw an exception.
if (m_oListenerThread.Join(302) == false)
{
m_oListenerThread.Abort();
}

if (m_oTransferThread != null)
{

//wait 20 ms for transfer thread to terminate. If the thread has not terminated by that time, abort thread and throw exception.
if (m_oTransferThread.Join(20) == false)
{
m_oTransferThread.Abort();
}
}

//some other termination logic
//...

}

so when x miliseconds has expired and the given thread is not terminated, Abort() is called on the thread to force it to shut down. The problem with the following is that when DisconnectFromServer is called by the listener thread (via HandleCommand()), the thread waits for the function to return so it may keep going and eventually exit its while loop. But it can never do this since DisconnectFromServer() will make it block (due to Join()). The thread is therefore stuck waiting for itself. When the time expires, it simply shuts itself down (using Abort()), and the function never returns, just abruptly stops.
This simply won't do! I have tried using an event in the hopes that the thread would fire a 'shutdown' event, and keep going while the main thread handles the event by calling DisconnectFromServer(), but the same problem arose: the thread waits for the event handler to return.
In short, is there a way to fire events or call methods without being forced to wait for those to return?
Or am I forced to create a third thread which would handle the shutdown procedure? Of course, if I do the latter, I won't really have any means to use Join() on that shutdown thread to make sure it terminated...
QuestionHelp: I'm new to C#: I want to make a global function Pin
TheFoZ19-Jun-08 8:27
TheFoZ19-Jun-08 8:27 
AnswerRe: Help: I'm new to C#: I want to make a global function Pin
Judah Gabriel Himango19-Jun-08 8:56
sponsorJudah Gabriel Himango19-Jun-08 8:56 
GeneralRe: Help: I'm new to C#: I want to make a global function Pin
TheFoZ19-Jun-08 9:03
TheFoZ19-Jun-08 9:03 
GeneralRe: Help: I'm new to C#: I want to make a global function Pin
Guffa19-Jun-08 11:20
Guffa19-Jun-08 11:20 
QuestionRadioButtonList Pin
john3419-Jun-08 7:59
john3419-Jun-08 7:59 
QuestionBuilding a System.Uri with a query string Pin
Spacix One19-Jun-08 7:40
Spacix One19-Jun-08 7:40 
AnswerRe: Building a System.Uri with a query string Pin
Judah Gabriel Himango19-Jun-08 8:24
sponsorJudah Gabriel Himango19-Jun-08 8:24 
GeneralRe: Building a System.Uri with a query string Pin
Spacix One19-Jun-08 9:06
Spacix One19-Jun-08 9:06 
GeneralRe: Building a System.Uri with a query string Pin
Judah Gabriel Himango19-Jun-08 11:05
sponsorJudah Gabriel Himango19-Jun-08 11:05 
GeneralRe: Building a System.Uri with a query string Pin
Spacix One19-Jun-08 13:00
Spacix One19-Jun-08 13:00 
QuestionOver ride GetReaderFromMessage method Pin
Toms Edison19-Jun-08 7:23
Toms Edison19-Jun-08 7:23 
QuestionSystem.Windows.Forms.Panel Pin
zvit19-Jun-08 6:33
zvit19-Jun-08 6:33 
AnswerRe: System.Windows.Forms.Panel Pin
Spacix One19-Jun-08 7:22
Spacix One19-Jun-08 7:22 
QuestionHow can we find the our system connected to the WEB ? Pin
Mohammad Dayyan19-Jun-08 5:02
Mohammad Dayyan19-Jun-08 5:02 
AnswerRe: How can we find the our system connected to the WEB ? Pin
Ashfield19-Jun-08 5:22
Ashfield19-Jun-08 5:22 
GeneralRe: How can we find the our system connected to the WEB ? Pin
Mohammad Dayyan19-Jun-08 5:30
Mohammad Dayyan19-Jun-08 5:30 
GeneralRe: How can we find the our system connected to the WEB ? Pin
Ashfield19-Jun-08 6:00
Ashfield19-Jun-08 6:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.