Synchronous socket and WaitHandle/WaitAny confusion
-
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 } }
-
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 } }
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![^] -
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![^]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