UDPClient.Receive And Thread Aborting...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a worker thread that listens for Datagrams using "data = listner.receive(receivedIPInfo)"
The problem im facing is that my Application wont close properly.
The worker thread runs a While loop. In my form closing im checking for the boolean variable that the while loop uses and setting it to FALSE so that the thread completes and hence my app terminates properlyl. The while loop variable gets set to false but the app still doesnt close properly.

I think its becase the thread has not passed the "data = listner.receive(receivedIPInfo)" statement and wont until it receives a datagram, therefore the while loop variable cant be examined. Is this the root of my problem? How do I solve it?

All advice is greatly appreciated.
Thanks in advance.
 
You may be able to close the socket on which the thread is waiting from the
main thread, after setting the boolean variable. This should cause the
receive to fail, at which point you can check the boolean and leave
gracefully.

Paul T.

Zahid said:
Hi,

I have a worker thread that listens for Datagrams using "data = listner.receive(receivedIPInfo)"
The problem im facing is that my Application wont close properly.
The worker thread runs a While loop. In my form closing im checking for
the boolean variable that the while loop uses and setting it to FALSE so
that the thread completes and hence my app terminates properlyl. The while
loop variable gets set to false but the app still doesnt close properly.
I think its becase the thread has not passed the "data =
listner.receive(receivedIPInfo)" statement and wont until it receives a
datagram, therefore the while loop variable cant be examined. Is this the
root of my problem? How do I solve it?
 
Hi

Thanks Paul. I tried your idea and does close the app. However it throws a System.Net.Sockets.SocketException. At the moment im catching the specific SocketException and doing nothing. Is this good practice? Is there an alternative way

Ive put the code to set the while loop variable in the onClosing of the form. When does the onClosing get fired? when you show another form like "DifferentForm.Show" does the onClosing get fired?

Thanks in advance.
 
Sure. That's totally what you'd expect to have happen. The socket has,
after all, been closed out from under a valid operation in another thread.
I'd catch the exception, check the Boolean value for thread exit and, if the
Boolean does *not* indicate that the thread should exit, figure out what to
do about the exception. If the thread should exit, exit.

I don't know enough about those sequences to answer this, but I wouldn't
expect bringing up another form to *close* the existing one...

Paul T.

Zahid said:
Hi,

Thanks Paul. I tried your idea and does close the app. However it throws
a System.Net.Sockets.SocketException. At the moment im catching the specific
SocketException and doing nothing. Is this good practice? Is there an
alternative way?
Ive put the code to set the while loop variable in the onClosing of the
form. When does the onClosing get fired? when you show another form like
"DifferentForm.Show" does the onClosing get fired?
 
Back
Top