How to cancel a BackgroundWorker in a blocked state ?

  • Thread starter Thread starter Sagaert Johan
  • Start date Start date
S

Sagaert Johan

The backgroundworker contains a blocking call to UDPClient.Receive

How do i unlock this thread so the backgroundworker can be cancelled ?

Johan
 
Johan,

I would call Dispose on the UdpClient. This will dispose of the client,
and probably shut down the socket.

You will have to prepare for this in your thread, as the Receive method
will probably throw.

Why are you blocking on Receive? Why not call BeginReceive and pass a
callback to it to be informed when it returns? This way, you don't have to
block a thread. It would be much more efficient.

Hope this helps.
 
thanks for the hint.

I got rid of the worker and switched to the async callback. ,works ok.

Nicholas Paldino said:
Johan,

I would call Dispose on the UdpClient. This will dispose of the client,
and probably shut down the socket.

You will have to prepare for this in your thread, as the Receive method
will probably throw.

Why are you blocking on Receive? Why not call BeginReceive and pass a
callback to it to be informed when it returns? This way, you don't have to
block a thread. It would be much more efficient.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Sagaert Johan said:
The backgroundworker contains a blocking call to UDPClient.Receive

How do i unlock this thread so the backgroundworker can be cancelled ?

Johan
 
Back
Top