How to create a WaitHandle on a native HANDLE?

N

nickdu

I'm using interop (PInvoke) to call a method
(FindFirstPrinterChangeNotification) which returns an event handle. I would
like to stay as much as possible in managed code so I want to get this native
event handle into a managed WaitHandle so I can call one of the WaitHandle's
Wait() methods. How do I do this?

The only way I see is to either:

1. Create an EventWaitHandle instance and then set the SafeWaitHandle
property. The problem with this route is that it will first create an event
object that I don't need.

2. Derive a class from WaitHandle so that I can get access to its protected
constructor which accepts a SafeWaitHandle.

Is there some other way? I was hoping there was an exposed way to create a
WaitHandle from a native event handle.
--
Thanks,
Nick

(e-mail address removed)
remove "nospam" change community. to msn.com
 
M

Mark Salsbery [MVP]

nickdu said:
I'm using interop (PInvoke) to call a method
(FindFirstPrinterChangeNotification) which returns an event handle. I
would
like to stay as much as possible in managed code so I want to get this
native
event handle into a managed WaitHandle so I can call one of the
WaitHandle's
Wait() methods. How do I do this?

The only way I see is to either:

1. Create an EventWaitHandle instance and then set the SafeWaitHandle
property. The problem with this route is that it will first create an
event
object that I don't need.

2. Derive a class from WaitHandle so that I can get access to its
protected
constructor which accepts a SafeWaitHandle.


I'd personally go with option 2, not necessarily just to get access to a
protected constructor, but because the WaitHandle class is designed to wrap
a native OS handle.

Seems like a simple, clean solution to me.

Mark
 
H

Hongye Sun [MSFT]

Hi Nick,

Thanks for your posting.

Personally, I agree with Mark that option #2 is the cleanest resolution for
this issue. I don't know any other better ways to do this job.

For your reference, here is an examle and it is similar to option #1:
http://www.codeproject.com/KB/printing/qPrintcomponent2.aspx

The difference is that it is using ManualResetEvent and
ThreadPool.RegisterWaitForSingleObject.

Hope it helps.

Regards,
Hongye Sun ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
 
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
 
Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top