What is wrong with this mutex code

  • Thread starter Thread starter Academia
  • Start date Start date
A

Academia

I build the solution.

In the bin folder I run the .exe

Twice

It runs each time.

Why doesn't the below code abort the second run?



Thanks

Shared Function Main(ByVal cmdArgs() As String) As Integer

Dim mutexWasCreated As Boolean

Using mutx As New Mutex(True, "TestJunk", mutexWasCreated)



If Not mutexWasCreated Then Return 1

End Using

Try

Application.EnableVisualStyles()

Application.Run(New FormStudioMdi)

Catch ex As Exception
 
Becaue you are releasing of the mutex way too early.

If you're going to have your Application.Run(...) in a Try/Catch/End Try
block then extend it to include a Finally section and release it there.
 
I build the solution.

In the bin folder I run the .exe

Twice

It runs each time.

Why doesn't the below code abort the second run?

Thanks

Shared Function Main(ByVal cmdArgs() As String) As Integer

Dim mutexWasCreated As Boolean

Using mutx As New Mutex(True, "TestJunk", mutexWasCreated)

If Not mutexWasCreated Then Return 1

End Using

Try

Application.EnableVisualStyles()

Application.Run(New FormStudioMdi)

Catch ex As Exception

"Finally" (optional) and "end try" (required) parts are missing. Also
you've defined "Catch" with no exception display method, you may
throw the exception.
 
I've never used a mutex so I'm going to finish the way I started and then
look at your suggestion.

The site you referenced says I need not release the mutex and also suggest
using a static variable.

Is that what you would do?


Thanks
 
Stephany Young said:
Becaue you are releasing of the mutex way too early.

If you're going to have your Application.Run(...) in a Try/Catch/End Try
block then extend it to include a Finally section and release it there.

I moved the End Using. Is that a good way?

Also, does it make sense to have the two message boxes.

I don't know a lot about the difference between two exceptions.

The revised code is below.



Thanks for the help

Shared Function Main(ByVal cmdArgs() As String) As Integer

Dim mutexWasCreated As Boolean

Using mutx As New Mutex(True, "TestJunk", mutexWasCreated)



If Not mutexWasCreated Then Return 1

Try

Application.EnableVisualStyles()

Application.Run(New FormStudioMdi)

Catch ex As Exception

Utility.WriteStackTrace(ex)

If ex.InnerException IsNot Nothing Then
MessageBox.Show(ex.InnerException.Message, " Experienced An Unhandled Inner
Error And Must Exit", MessageBoxButtons.OK)

If ex.GetBaseException IsNot Nothing Then
MessageBox.Show(ex.GetBaseException.Message, " Experienced A BaseException
And Must Exit", MessageBoxButtons.OK)

Return 1

End Try

Return 0

End Using

End Function
 
I should have sent
Catch ex As Exception ..snip...
There is code I didn't show.
Maybe you could comment on my reply to Stephany Young which shows all the
code.

Thanks
 
Academia said:
The site you referenced says I need not release the mutex and also suggest
using a static variable.

Is that what you would do?

Yes.
 
Back
Top