471,310 Members | 1,058 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Threads - ask for advice

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

Dec 1 '05 #1
2 981
Piotrekk wrote:
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
Dec 1 '05 #2
Hi,

Run this method from a thread marked with IsBackground = true
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Piotrekk" <Pi*************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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();
}
}

Dec 1 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Mark Stokes | last post: by
1 post views Thread by RahimAsif | last post: by
6 posts views Thread by RahimAsif | last post: by
3 posts views Thread by Anders Both | last post: by
6 posts views Thread by Gary Lee | last post: by
10 posts views Thread by Brad | last post: by
2 posts views Thread by Matthew Tylee Atkinson | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.