What is wrong with this mutex code

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
 
S

Stephany Young

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.
 
K

kimiraikkonen

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.
 
A

Academia

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
 
A

Academia

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
 
A

Academia

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
 
H

Herfried K. Wagner [MVP]

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.
 

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