Threads - ask for advice

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();
}
}
 
M

Markus Stoeger

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Run this method from a thread marked with IsBackground = true


cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top