Thread unloads fine in "DOS" not Windows

  • Thread starter Thread starter M D
  • Start date Start date
M

M D

If you want more details you will have to reference the VS.Net example
ConsoleChat for Networking How-to:
--
http://go.microsoft.com/fwlink/?linkid=3480&path=/quickstart/howto/sampl
es/net/TCPUDP/Chat.src

In trying to put it into a Windows form (c#.net, VS 2002) this is my
main:

[STAThread]
static void Main() {
Thread t =
new Thread(new ThreadStart(chatter.Listener));
try {
chatter.Initialize();
t.Start();
}
catch (Exception cex) {
chatter.Terminate();
MessageBox.Show(cex.Message);
if (t.IsAlive) {
t.Abort();
t.Join();
}
}

Application.Run(new Form1());

chatter.Terminate();
MessageBox.Show("Alive: "+t.IsAlive);
t.Abort();
MessageBox.Show("Alive: "+t.IsAlive);
t.Join();
MessageBox.Show("Alive: "+t.IsAlive);
}

--------------------
chatter is the class for the chat util where all this activity is
static--no instantiation of a chatter. (I know, it s/b capitalized.)

The only modifications to the code is the removal of Console.ReadLine
stuff since I now have a textbox. Also, chatter.Terminate() sets the
flag field which breaks the while loop within chatter.Listener().

It actually all works. However, the Thread refuses to unload. The last
3 MessageBoxes come back True, True, ...(never pops the last one). I
have to go to the TaskManager and kill the process.

I can get it to unload. If I comment out the:

chatter.Chatter += new MessageEventHandler(this.addChat);

which, of course, makes it stop handling the messages AND, (let me
repeat that) AND actually type a [return] into the textbox which,
coindidentally fires a KeydownEventHandler to trap the [return]. So
even disconnected from the chatter the thread sticks UNLESS I trigger
the other event handler which is entirely within the Windows Form.

Anybody have a clue for me?

thx
md
 
I got it.

The DOS version works because the flag governing the while loop in the
listener is flipped by a message received by the listener. When I
included a 'bye' message in the closing process, the while loop
completed allowing the thread to abort.

thx
md
 
Back
Top