Threads - ask for advice

  • Thread starter Thread starter Piotrekk
  • Start date Start date
P

Piotrekk

Hi.
I have written client and server app. that sends files.
Client application doesn't use threads, but server does.
I would like to show you part of my code ( thread) and ask why, when i
close program, it's still visible in processes.
I have also created bool variable "closing", by which thread recognize
if program is quit ( set "closing = true" in dispose method )


private void thd()
{
TcpListener Server = new TcpListener(5000);

try
{
Server.Start();
}
catch (SocketException ex)
{
if (MessageBox.Show(ex.Message, "tcpListener.Start()
error",
MessageBoxButtons.OK, MessageBoxIcon.Error) ==
DialogResult.OK) this.Dispose();
}

while (!closing && Server!=null)
{
Socket connection = Server.AcceptSocket();

if (connection!= null && connection.Connected)
{
String fName = rFileName(connection);
storeFile(connection, fName);
}
GC.Collect();
}
}
 
Piotrekk said:
Hi.
I have written client and server app. that sends files.
Client application doesn't use threads, but server does.
I would like to show you part of my code ( thread) and ask why, when i
close program, it's still visible in processes.
I have also created bool variable "closing", by which thread recognize
if program is quit ( set "closing = true" in dispose method )

Just a guess... the server thread is still blocked in
Server.AcceptSocket(), so setting "closing" to true doesn't help much
because it won't event see it. You can verify this with the debugger,
click break and open the threads window.

I'd try doing this with async sockets or using Socket.Poll to wait for
connections.
GC.Collect();

Thats normally unnecessary..

hth,
Max
 
Hi,

Run this method from a thread marked with IsBackground = true


cheers,
 
Back
Top