Hi All,
I wanted to use WaitForMultipleObjects in C#. Following is the
code:
[DllImport("kernel32.dll", SetLastError=true)]
static extern int WaitForMultipleObjects ( int nCount, ref IntPtr
[]lpHandles, int bWaitAll, uint dwMilliseconds);
...
IntPtr []HandleArray = new IntPtr[2];
HandleArray[0] = (IntPtr)m_iStopEventHandle;
HandleArray[1] = (IntPtr)m_iTimerHandle;
int iRet = WaitForMultipleObjects(2, ref HandleArray, 1, INFINITE);
...
I am getting -1 (Invalid handle) as the return value.
If I use the 2 handles( m_iStopEventHandle and m_iTimerHandle) then
WaitForMultipleObjects works fine.
Please let me know if I am doing anything wrong with the pinvoke.
Thanks in advance.
nachiket
Ming Chen - 14 Nov 2005 14:35 GMT
Hi,
Arrays are marshalled to pointers by default, so you don't have to add
"ref" in the parameter.
[DllImport("kernel32.dll")]
static extern uint WaitForMultipleObjects(uint nCount, IntPtr [] lpHandles,
bool bWaitAll, uint dwMilliseconds);
Hope this helps.
> Hi All,
> I wanted to use WaitForMultipleObjects in C#. Following is the
[quoted text clipped - 17 lines]
> Thanks in advance.
> nachiket
TT (Tom Tempelaere) - 14 Nov 2005 16:12 GMT
Hi Nachiket,
> Hi All,
> I wanted to use WaitForMultipleObjects in C#. Following is the
[quoted text clipped - 17 lines]
> Thanks in advance.
> nachiket
I fail to see why you didn't use the WaitHandle class to wait on multiple
"waitable" objects, in specific WaitHandle.WaitAll. Except of course you're
doing interop and have handles that are created outside the .NET framework.
Kind regards,
--
Tom Tempelaere.
nachiket_pict_2002@yahoo.co.in - 15 Nov 2005 12:12 GMT
Thanks for the reply.
I have not used WaitHandle since I have created the event and the timer
using p-invoke
nachiket_pict_2002@yahoo.co.in - 15 Nov 2005 12:13 GMT
Thanks for the reply.
I have not used WaitHandle since I have created the event and the timer
using p-invoke.
Regards,
Nachiket
TT (Tom Tempelaere) - 15 Nov 2005 17:50 GMT
Hi Nachiket,
> Thanks for the reply.
> I have not used WaitHandle since I have created the event and the timer
> using p-invoke.
>
> Regards,
> Nachiket
So why didn't you? You're in .NET environment and using C# aren't you? Then
if you don't need the handles for interop (towards COM or dll's) then I
advise you to use the waitable classes in the .NET framework (eg Mutex,
ManualResetEvent, ...) and wait using the WaitXXX functions in the
WaitHandle class. Anything you'd need is in System.Threading. On the other
hand if you have third party libraries that require these handles then you
have no choice, but from what you posted up to now my guess is that you're
not in that situation. Am I correct?
Kind regards,
--
Tom Tempelaere.