Problems with WaitForMultipleObjects under C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Under WinCe Win32 we are experiencing problems with
"WaitForMultipleObjects(DWORD nCount, CONST HANDLE* lpHandles, BOOL fWaitAll,
DWORD dwMilliseconds )" being accessed from C#. It returns WAIT_FAILED and a
call to GetLastError() returns 0x80000005 (Invalid pointer).

We are trying to implement the function "WaitHandle.WaitAny(commands);"
which is not available in Netcf. We are using and extending the OpenNetcf
open source code.

The top call in its own thread looks like this--
ManualResetEvent [] commands = { channelEvent, killEvent };
int eventThatFired = 0;
bool keepGoing = true;

while (keepGoing)
{
eventThatFired = Core.WaitForMultipleObjectsJ1(commands,false);

The code we added in OpenNetcf looks like this--
[CLSCompliant(false)]
public static Int32 WaitForMultipleObjectsJ1(ManualResetEvent [] commands,
bool WaitForAll)
{
Int32 Len=commands.Length;
if(commands==null||Len==0)
{return 0;
}
IntPtr[] handles=new IntPtr[Len];
for(Int32 i=0;i<Len;i++)
{
handles=commands.Handle;
}
return
OpenNETCF.Threading.NativeMethods.WaitForMultipleObjects((uint)handles.Length, handles, WaitForAll, (uint)0xFFFFFFFF);
}
OpenNetcf uses this PInvoke--
[DllImport("coredll.dll", SetLastError=true)]
public static extern int WaitForMultipleObjects(uint nCount, IntPtr[]
lpHandles, bool fWaitAll, uint dwMilliseconds);
The symptom is that the call "
Core.WaitForMultipleObjectsJ1(commands,false);" returns immediately with a
return value of -1. This works back (using the GetLastError()) to the invalid
pointer error code.

Each ManualResetEvent instance does block properly if invoked usng the
WaitOne() method which is implemented in Netcf as below--
bool xbool=channelEvent.WaitOne();

Any help would be appreciated on this problem.

Thanks.
 
The ManualResetEvent doesn't expose an actual handle, but a pseudohandle, so
it cannot be waited on through a P/Invoke. If you want to use
WaitForMultipleObjects, you'll have to P/Invoke CreateEvent and use the
handle returned from it.

-Chris
 
Thanks for the information.

I assume that somewhere inside ManualResetEvent there is an actual Win32
handle. Is there no way to access this handle. We have been using OpenNetcf
to "fill in the gaps" on Netcf if that adds any possibilities.

Just want to be sure before changing the code.

Thanks again.
--
John Olbert



The ManualResetEvent doesn't expose an actual handle, but a pseudohandle, so
it cannot be waited on through a P/Invoke. If you want to use
WaitForMultipleObjects, you'll have to P/Invoke CreateEvent and use the
handle returned from it.

-Chris


John Olbert said:
Under WinCe Win32 we are experiencing problems with
"WaitForMultipleObjects(DWORD nCount, CONST HANDLE* lpHandles, BOOL
fWaitAll,
DWORD dwMilliseconds )" being accessed from C#. It returns WAIT_FAILED
and a
call to GetLastError() returns 0x80000005 (Invalid pointer).

We are trying to implement the function "WaitHandle.WaitAny(commands);"
which is not available in Netcf. We are using and extending the OpenNetcf
open source code.

The top call in its own thread looks like this--
ManualResetEvent [] commands = { channelEvent, killEvent };
int eventThatFired = 0;
bool keepGoing = true;

while (keepGoing)
{
eventThatFired = Core.WaitForMultipleObjectsJ1(commands,false);

The code we added in OpenNetcf looks like this--
[CLSCompliant(false)]
public static Int32 WaitForMultipleObjectsJ1(ManualResetEvent [] commands,
bool WaitForAll)
{
Int32 Len=commands.Length;
if(commands==null||Len==0)
{return 0;
}
IntPtr[] handles=new IntPtr[Len];
for(Int32 i=0;i<Len;i++)
{
handles=commands.Handle;
}
return
OpenNETCF.Threading.NativeMethods.WaitForMultipleObjects((uint)handles.Length,
handles, WaitForAll, (uint)0xFFFFFFFF);
}
OpenNetcf uses this PInvoke--
[DllImport("coredll.dll", SetLastError=true)]
public static extern int WaitForMultipleObjects(uint nCount, IntPtr[]
lpHandles, bool fWaitAll, uint dwMilliseconds);
The symptom is that the call "
Core.WaitForMultipleObjectsJ1(commands,false);" returns immediately with a
return value of -1. This works back (using the GetLastError()) to the
invalid
pointer error code.

Each ManualResetEvent instance does block properly if invoked usng the
WaitOne() method which is implemented in Netcf as below--
bool xbool=channelEvent.WaitOne();

Any help would be appreciated on this problem.

Thanks.
 
The standard CF control surely holds a handle, but it's private so getting
it would require reflection. In the SDF it's a lot easier - the
OpenNETCF.Threading.EventWaitHandle gets you most of the way there.

--
Chris Tacke
Co-founder
OpenNETCF.org
Are you using the SDF? Let's do a case study.
Email us at d c s @ o p e n n e t c f . c o m
http://www.opennetcf.org/donate


John Olbert said:
Thanks for the information.

I assume that somewhere inside ManualResetEvent there is an actual Win32
handle. Is there no way to access this handle. We have been using
OpenNetcf
to "fill in the gaps" on Netcf if that adds any possibilities.

Just want to be sure before changing the code.

Thanks again.
--
John Olbert



The ManualResetEvent doesn't expose an actual handle, but a pseudohandle,
so
it cannot be waited on through a P/Invoke. If you want to use
WaitForMultipleObjects, you'll have to P/Invoke CreateEvent and use the
handle returned from it.

-Chris


John Olbert said:
Under WinCe Win32 we are experiencing problems with
"WaitForMultipleObjects(DWORD nCount, CONST HANDLE* lpHandles, BOOL
fWaitAll,
DWORD dwMilliseconds )" being accessed from C#. It returns WAIT_FAILED
and a
call to GetLastError() returns 0x80000005 (Invalid pointer).

We are trying to implement the function "WaitHandle.WaitAny(commands);"
which is not available in Netcf. We are using and extending the
OpenNetcf
open source code.

The top call in its own thread looks like this--
ManualResetEvent [] commands = { channelEvent, killEvent };
int eventThatFired = 0;
bool keepGoing = true;

while (keepGoing)
{
eventThatFired =
Core.WaitForMultipleObjectsJ1(commands,false);

The code we added in OpenNetcf looks like this--
[CLSCompliant(false)]
public static Int32 WaitForMultipleObjectsJ1(ManualResetEvent []
commands,
bool WaitForAll)
{
Int32 Len=commands.Length;
if(commands==null||Len==0)
{return 0;
}
IntPtr[] handles=new IntPtr[Len];
for(Int32 i=0;i<Len;i++)
{
handles=commands.Handle;
}
return
OpenNETCF.Threading.NativeMethods.WaitForMultipleObjects((uint)handles.Length,
handles, WaitForAll, (uint)0xFFFFFFFF);
}
OpenNetcf uses this PInvoke--
[DllImport("coredll.dll", SetLastError=true)]
public static extern int WaitForMultipleObjects(uint nCount, IntPtr[]
lpHandles, bool fWaitAll, uint dwMilliseconds);
The symptom is that the call "
Core.WaitForMultipleObjectsJ1(commands,false);" returns immediately
with a
return value of -1. This works back (using the GetLastError()) to the
invalid
pointer error code.

Each ManualResetEvent instance does block properly if invoked usng the
WaitOne() method which is implemented in Netcf as below--
bool xbool=channelEvent.WaitOne();

Any help would be appreciated on this problem.

Thanks.
 
Back
Top