Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Mutex creation in vb

0 views
Skip to first unread message

Paul_S

unread,
Sep 7, 2004, 9:09:09 AM9/7/04
to
Hi,

I'm using the code at the end of this msg to create a mutex. I've put a
watch expression on 'CreateMutex(0, False, conMutexName)' and a breakpoint on
the same line (line 20) Then stepping through the sub routine, I've noticed
that the watch value is changing on everyline I step through rather than just
changing when it reaches that line 20? Once it hits the breakpoint the watch
expression may have a value of say 250. As I step over this point, the value
is recreated and assigned to RetVal, making RetVal say 255. If I continue
through the code, it fails on line 40, because the value it requires is 250
not 255. I have tested this by overtyping HMUTEX in line 20 with the value
of (in this case) 250 and it then hits line 30 and releases the mutex?????????

How can I correct this?

Also, each time I step through this process it is creating a new mutex that
never gets the closehandle command. do these threads get killed when the
calling app terminates or are they just drifing around?

Thanks a ton to anyone who can help!
Paul


Private Const WAIT_ABANDONED As Integer = &H80
Private Const WAIT_FAILED As Integer = -1
Private Const WAIT_OBJECT_0 As Integer = 0
Private Const WAIT_TIMEOUT As Integer = &H102

Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA"
(ByVal lpMutexAttributes As Integer, ByVal bInitialOwner As Integer, ByVal
lpName As String) As Integer
Private Declare Function ReleaseMutex Lib "kernel32" (ByVal hMutex As
Integer) As Integer
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As
Integer) As Integer
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal
hHandle As Integer, ByVal dwMilliseconds As Integer) As Integer

Private Const conWaitDelay_ms As Integer = 30000 '

Sub Mut()
Dim hMutex As Integer
Dim retVal As Integer

On Error GoTo ProcessErr
10
hMutex = (CreateMutex(0, False, conMutexName))
20 retVal = WaitForSingleObject(hMutex, conWaitDelay_ms)

Select Case retVal
Case WAIT_OBJECT_0
30 ReleaseMutex (hMutex)
40 Case WAIT_FAILED
Case WAIT_TIMEOUT
Case WAIT_ABANDONED
Case Else
End Select

CloseHandle (hMutex)

Exit Sub

ProcessErr:
MsgBox Err.LastDllError
End Sub

Björn Holmgren

unread,
Sep 7, 2004, 9:32:11 AM9/7/04
to
"Paul_S" <Pau...@discussions.microsoft.com> wrote in message
news:50C083EF-B1C0-487A...@microsoft.com...

> Hi,
>
> I'm using the code at the end of this msg to create a mutex. I've put a
> watch expression on 'CreateMutex(0, False, conMutexName)' and a breakpoint
on
> the same line (line 20) Then stepping through the sub routine, I've
noticed
> that the watch value is changing on everyline I step through rather than
just
> changing when it reaches that line 20? Once it hits the breakpoint the
watch
> expression may have a value of say 250. As I step over this point, the
value
> is recreated and assigned to RetVal, making RetVal say 255. If I continue
> through the code, it fails on line 40, because the value it requires is
250
> not 255. I have tested this by overtyping HMUTEX in line 20 with the
value
> of (in this case) 250 and it then hits line 30 and releases the
mutex?????????
>
> How can I correct this?
>

If you put a watch expression on 'CreateMutex(0, False, conMutexName)',
won't that cause the CreateMutex function to be called every time the watch
expression is re-evaluated, thus constantly creating new mutex objects?
Shouldn't you set the watch expression to the hMutex variable instead?

--
Björn Holmgren

0 new messages