Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2005

Tip: Looking for answers? Try searching our database.

WaitForMultipleObjects in .net

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nachiket_pict_2002@yahoo.co.in - 14 Nov 2005 13:17 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.