Question

I have an MFC/C++ API which utilizes handles and WaitForSingleObject (or, potentially, WaitForMultipleObject). I'd like to call those functions from a C# frontend, but I'm having trouble determining how to go about doing this.

For example, in my MFC sample app, I do the following:

request_handle = CreateEvent(NULL,TRUE,FALSE,NULL);
WorkFunction(blah, request_handle); //signals the event when it's done
x=WaitForSingleObject(request_handle,timeout);
switch(x)
{
    case WAIT_OBJECT_0:
    ...
    break;
    case WAIT_TIMEOUT:
    ...
    break;
}

Specifically, my question is how do I create request_handle and pass it to the MFC function WorkFunction and then wait for the event in C#?

Was it helpful?

Solution

The exact equivalent to your CreateEvent() call is a ManualResetEvent in C# code. Pass its Handle property to your native code.

The exact equivalent to your WaitForSingleObject() call is ManualResetEvent.WaitOne(int) in C#. It returns false if the timeout expired.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top