I need to replicate the following C++ function in C#:
void TComThread::CommWaitForAndHandleMessages(int minimum_poll)
{
// Wait for:
// Any of the HANDLES in the WaitFor array, OR
// Any Windows message, OR
// Any Asynchronous Procedure Call (APC) to complete OR
// A timeout after minimum_poll milliseconds
const DWORD ret = MsgWaitForMultipleObjectsEx(0,
NULL, minimum_poll, QS_ALLINPUT, MWMO_ALERTABLE);
// APC: it was already called, so we just return
if (ret == WAIT_IO_COMPLETION)
return;
// Timeout: we just return
else if (ret == WAIT_TIMEOUT)
return;
// Windows message: process messages and return
else if (ret == WAIT_OBJECT_0)
Application->ProcessMessages();
// Error of some kind
else
RaiseLastWin32Error();
}
I believe the key to this function is finding a C# equivalent to MsgWaitForMultipleObjectsEx. So far the closest method I've found in C# that emulates this behavior is the WaitHandle.WaitAny() method. With WaitAny()
I can set a timer and also specify WaitHandle
objects for which the current instance will wait. However, my trouble is with the dwWakeMask and dwFlags values because I can't seem to match them with any objects in System.Threading
. I'm also concerned with WAIT_IO_COMPLETION
and WAIT_OBJECT_0
.
This is my C# code so far:
private void CommWaitForAndHandleMessages(int minimum_poll)
{
Thread.WaitHandle[] waitHandles = new Thread.WaitHandle[]
{
//This is where I would create my WaitHandle array
};
int ret = Thread.WaitHandle.WaitAny(/* BLANK **/, new TimeSpan(minimum_poll), false);
//Timeout -- return
if(ret == Thread.WaitHandle.WaitTimeout)
return;
}
I've read about WaitHandle.SafeWaitHandle and that it "Gets or sets the native operating system handle". Am I able to use SafeWaitHandle
to obtain access to native values like QS_ALLINPUT
? If not, how can I add to my approach to replicate the behavior of the C++ function?
Note: I have to do this without P/Invoke or any sort of wrapper.