Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Async model question

Async model question

Scheduled Pinned Locked Moved C#
questionhelp
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ryan Cromwell
    wrote on last edited by
    #1

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

    E 1 Reply Last reply
    0
    • R Ryan Cromwell

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

      E Offline
      E Offline
      Eric Gunnerson msft
      wrote on last edited by
      #2

      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?

      R 1 Reply Last reply
      0
      • E Eric Gunnerson msft

        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?

        R Offline
        R Offline
        Ryan Cromwell
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups