Async model question
-
What is the suggested thread model for a Listener class? My particular situation involves an asyncronous multicast client that continuously listens for available data and notifies via delegates of received data. The problem is the receive function becomes a blocker if the sender no longer publishes data and the ListenHelper function will never progress. I realize that I could Abort the thread, but for some reason that seems like poor solution. Maybe not. See the snippet below: class AsyncMCListener { BeginListen { _thListener = new Thread( new ThreadStart( ListenHelper ) ); _thListener.IsBackground = true; _thListener.Priority = ThreadPriority.BelowNormal; _thListener.Start(); } ListenHelper { while( !_bDone ) { ReceiveData //BLOCKING CALL NotifyConsumerDataReady } } EndListen { _bDone = true; } }
-
What is the suggested thread model for a Listener class? My particular situation involves an asyncronous multicast client that continuously listens for available data and notifies via delegates of received data. The problem is the receive function becomes a blocker if the sender no longer publishes data and the ListenHelper function will never progress. I realize that I could Abort the thread, but for some reason that seems like poor solution. Maybe not. See the snippet below: class AsyncMCListener { BeginListen { _thListener = new Thread( new ThreadStart( ListenHelper ) ); _thListener.IsBackground = true; _thListener.Priority = ThreadPriority.BelowNormal; _thListener.Start(); } ListenHelper { while( !_bDone ) { ReceiveData //BLOCKING CALL NotifyConsumerDataReady } } EndListen { _bDone = true; } }
I can't think of an obvious way to tell the difference between "there's no more data coming in" and "there will be data in a while", so I think you're stuck with the loop you have. Can you explain why the loop being blocked on the receive is a problem?
-
I can't think of an obvious way to tell the difference between "there's no more data coming in" and "there will be data in a while", so I think you're stuck with the loop you have. Can you explain why the loop being blocked on the receive is a problem?
The problem arises when the client wants to stop listening. They've decided they no longer want to be a part of the chat session or the video stream or in my case the Trace/Debug broadcast. There very well may be more data "in a while", but the client/subscriber has chosen not to receive that data. I've implemented the Listener loop such that it handles ThreadAbort and does the socket.Shutdown and socket.Close in the finally of try...catch, but it doesn't seem right. I feel as if there should be an interrupt in there somewhere in the ReceiveFrom call on the socket. That's about the only other way to do it, but that's not built into the socket class, so oh well. Anyways, here's what we've got instead...