How To: WaitAny() in .NET Compact Framework

This is for the .NET Compact Framework developers out there!

For the past few weeks, I’ve been porting a desktop application that was heavy on threading to a mobile application.  One of my biggest problems was to make the UI on the mobile device a bit more responsive.  Much to my surprise this was not as easy as I thought.  The .NET CF 2.0 now provides a WaitOne() method with a timeout as a parameter but that wouldn’t solve my problem since I need to manage multiple threads and not just one.  There’s a WaitAny() method in the full .NET Framework but this wasn’t available in the .NET CF. I needed the functionality of a WaitAny() method in the .NET Framework and so I searched the web for a solution to my problem.  After a few days of searching, I still wasn’t able to find one that would meet my needs.  The closest that I found was this post on the OpenNETCF forums, it uses a P/Invoke call to WaitForMultipleObjects() but it simply doesn’t work when I pass my array of handles as it immediately returns a value of -1. 

And so I thought what if the P/Invoke call doesn’t recognize the handles I’m passing to it.  Maybe I could use another P/Invoke call to create those handles so that it’ll be able to recognize it.  So I searched again and found a way to create those WaitHandle events through P/Invoke. 

Here’s my solution for it:

List of P/Invokes:


[DllImport("coredll.dll", CharSet = CharSet.Unicode, SetLastError = false)]
public static extern int WaitForMultipleObjects(int count, IntPtr[] handle, bool waitAll, int milliseconds);
[DllImport("coredll.dll", CharSet = CharSet.Unicode, SetLastError = false)]
public static extern bool CloseHandle(IntPtr handle);
[DllImport("coredll.dll", CharSet = CharSet.Unicode, SetLastError = false)]
public static extern IntPtr CreateEvent(IntPtr eventAttributes, bool isManualReset, bool initialState, string eventName);

[DllImport("coredll.dll", SetLastError = false)]
public static extern int EventModify( IntPtr handle, uint eventAction );

The WaitAny() method implementation:

public static int WaitAny(IntPtr[] waitHandleArray, int milliseconds, bool waitForAll)
{

int waitHandleCount = waitHandleArray.Length;

if(waitHandleArray == null || waitHandleCount == 0)
    return -2;

return WaitForMultipleObjects(waitHandleCount, waitHandleArray, waitForAll, milliseconds);
}

So what you do is when you create a WaitHandle event you use the CreateEvent method and it would return a handle to the event created.  The WaitAny method would wait for one event to be set in the array of event handles passed to it and return the index of the event in the array. You set an event using the EventModify method.  And after you’re done with all of the WaitHandle events make sure that you release them using the CloseHandle method.

You might consider this as an alternative until the next release of .NET CF which (hopefully) the .NET CF team might include very useful methods like the WaitAny().

One Response to “How To: WaitAny() in .NET Compact Framework”

  1. Life's ups and more ups! ^_^ Says:

    Hi!

    Hello spokedom. :D It’s been a while since I visited this place. 
    Anyways, just wanted to introduce…


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.