...

View Full Version : Threading paradox in C#



Time Sheep
09-01-2012, 09:24 PM
Hey guys!
I am working on a little program for handling TCP connections, and as you may or may not know, execution of later code is blocked while the program is waiting for a connection to accept (TcpClient.TcpClientAccept()).
I tried to do a little work around, which I assume is also the best way to run listening servers, by starting a seperate thread... However Visual C# denies to compile the code, since listenThread is instantiated in a function that isn't necessarily called when listenThread.Abort() is called.
Well, what the heck, here's my code. btnListen and btnStop are the start and stop buttons obviously.

private void btnListen_Click (object sender, EventArgs e) {
btnListen.Enabled = false;
btnStop.Enabled = true;
Thread listenThread = new Thread ( new ThreadStart(Server.winMain.Listen) );
}

private void btnStop_Click ( object sender, EventArgs e ) {
btnListen.Enabled = true;
btnStop.Enabled = false;
listenThread.Abort ();
serverSocket.Stop ();
}
Visual C# 2010 underlines listenThread.Abort(); with the error "listenThread does not exist in the current context, which makes sense to me. But how do I get around this?

EDIT: Also, even though really offtopic, what does [somethingsomething] mean? Is it labels or what? I have no idea how to search for this...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum