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. Synchronous socket and WaitHandle/WaitAny confusion

Synchronous socket and WaitHandle/WaitAny confusion

Scheduled Pinned Locked Moved C#
questioncsharpc++regexhelp
3 Posts 3 Posters 1 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.
  • J Offline
    J Offline
    Jerry Evans
    wrote on last edited by
    #1

    Struggling to adapt a C++ pattern to C#. How do I get a WaitHandle from an IntPtr, which is the underlying socket handle to use in the socket thread as outlined below? I know there are Aysnc socket varieties but I'm keen to reuse this if at all possible. *** indicates the problem area in code Thx++ Jerry // AutoResetEvent evtHalt = new AutoResetEvent; // Socket socket = new Socket(...); // WaitHandle handles = new WaitHandle(2); // set up handles handles[0] = evtHalt; // *** Nope *** conversion problems - handles[1] = socket.Handle; // while (true) { Int32 ret = WaitHandle.WaitAny(handles); if (ret == 0) { // cancelled as evtHalt was signalled break; } else if (ret == 1) { // socket signalled ... do something } }

    G 1 Reply Last reply
    0
    • J Jerry Evans

      Struggling to adapt a C++ pattern to C#. How do I get a WaitHandle from an IntPtr, which is the underlying socket handle to use in the socket thread as outlined below? I know there are Aysnc socket varieties but I'm keen to reuse this if at all possible. *** indicates the problem area in code Thx++ Jerry // AutoResetEvent evtHalt = new AutoResetEvent; // Socket socket = new Socket(...); // WaitHandle handles = new WaitHandle(2); // set up handles handles[0] = evtHalt; // *** Nope *** conversion problems - handles[1] = socket.Handle; // while (true) { Int32 ret = WaitHandle.WaitAny(handles); if (ret == 0) { // cancelled as evtHalt was signalled break; } else if (ret == 1) { // socket signalled ... do something } }

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      Why don't you want to use the built-in .NET socket stuff? It's so easy it's downright sinful. We have a proprietary client/server protocol and a diagnostic tracing facility built on top of TCP/IP sockets. The original code, written in C++, probably has close to a man-year of time on it. I replicated both of them in C# in a couple of weeks.

      Software Zen: delete this;
      Fold With Us![^]

      J 1 Reply Last reply
      0
      • G Gary R Wheeler

        Why don't you want to use the built-in .NET socket stuff? It's so easy it's downright sinful. We have a proprietary client/server protocol and a diagnostic tracing facility built on top of TCP/IP sockets. The original code, written in C++, probably has close to a man-year of time on it. I replicated both of them in C# in a couple of weeks.

        Software Zen: delete this;
        Fold With Us![^]

        J Offline
        J Offline
        Jorgen Sigvardsson
        wrote on last edited by
        #3

        Sometimes you want to wait for other things than socket handles. It's common to have EventWaitHandles for signalling conditions to threads. If a thread needs to monitor both sockets and events, then one must find a way to do it the "C++ way". Still looking. :(

        -- Kein Mitleid Für Die Mehrheit

        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