Developer, Former MVP, now at Microsoft - Best of 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
/// <summary>
/// This function opens a handle to an existing message queue based on
/// a message queue handle.
/// </summary>
/// <param name="hSrcProc">[in] Handle to a source process that owns the
/// hMsgQ message queue handle. </param>
/// <param name="hMsgQ">[in] Handle to a message queue returned by the
/// CreateMsgQueue function.</param>
/// <param name="lpOptions">[in] Pointer to an MSGQUEUEOPTIONS structure
/// that sets the properties of the message queue.</param>
/// <returns>Returns a handle to a message queue, or returns NULL if the
/// queue could not be opened.</returns>
[DllImport("coredll.dll", SetLastError=true)]
public static extern IntPtr OpenMsgQueue(IntPtr hSrcProc, IntPtr hMsgQ,
MsgQueueOptions lpOptions);
Private Function DoWork2() As Boolean
' If I don't have a requester then I can't do anything with the input
' so ignore it (and don't obtain the lock!)
Dim req As ForMyPending = Nothing
req = Me.FindFreeRequester
If req Is Nothing Then
Return False
End If
' Get the next object from the queue
Dim queObj As ForMyQueue = Nothing
SyncLock mQue.SyncRoot
If mQue.Count > 0 Then
queObj = DirectCast(mQue.Dequeue(), ForMyQueue)
End SyncLock
' Deal with the next object from the queue without holding the lock!
If Not queObj Is Nothing Then
If queObj.ReqArgs.MaxNumResponses <> 0 Then'chance for client to cancel
req.Deleg = queObj.Deleg
If req.Requester.Request(queObj.ReqArgs, queObj.State) = False Then
If Not req.Deleg Is Nothing Then
req.Deleg.Invoke(Me, ReplyArgs.Empty)'failed
req.Deleg = Nothing'otherwise it wil never be reused again!
Return True
End Function