How to cancel a BackgroundWorker in a blocked state ?

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
S

Sagaert Johan

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
 

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