473,407 Members | 2,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

What is wrong with this mutex code

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
Jan 2 '08 #1
8 1386
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.
"Academia" <ac************@a-znet.comwrote in message
news:uG**************@TK2MSFTNGP03.phx.gbl...
>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

Jan 2 '08 #2
On Jan 2, 5:17 am, "Academia" <academiaNOS...@a-znet.comwrote:
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.
Jan 2 '08 #3
"Academia" <ac************@a-znet.comschrieb:
In the bin folder I run the .exe

Twice

It runs each time.

Why doesn't the below code abort the second run?
<URL:http://www.yoda.arachsys.com/csharp/faq/#one.application.instance>

In VB 2005 and VB 2008 you can make your application single-instance by
enabling the application framework and checking a checkbox in the
application framework properties (available in "My Project").

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jan 2 '08 #4
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
"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:uG**************@TK2MSFTNGP02.phx.gbl...
"Academia" <ac************@a-znet.comschrieb:
>In the bin folder I run the .exe

Twice

It runs each time.

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

<URL:http://www.yoda.arachsys.com/csharp/faq/#one.application.instance>

In VB 2005 and VB 2008 you can make your application single-instance by
enabling the application framework and checking a checkbox in the
application framework properties (available in "My Project").

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jan 2 '08 #5

"Stephany Young" <noone@localhostwrote in message
news:e4**************@TK2MSFTNGP05.phx.gbl...
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
Jan 2 '08 #6

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
>
"Finally" (optional) and "end try" (required) parts are missing. Also
you've defined "Catch" with no exception display method, you may
throw the exception.

Jan 2 '08 #7
"Academia" <ac************@a-znet.comschrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jan 2 '08 #8
thanks
"Herfried K. Wagner [MVP]" <hi***************@gmx.atwrote in message
news:uw**************@TK2MSFTNGP02.phx.gbl...
"Academia" <ac************@a-znet.comschrieb:
>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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Jan 2 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bastian Hammer | last post by:
Hi I´m wondering why there are so few examples with Semaphore. Is it obsolete? I´ve got a Class Data. It offers 2 Threads methods for updating, editing, .. a private dictionary. Now I...
4
by: TGOS | last post by:
I was thinking about it for a while, a mutex written in C and without disabling any interrupts. Is it possible? typdef struct mutex { unsigned int owner1; unsigend int owner2; } *mutex; ...
4
by: Brian W | last post by:
Hi all, I'm working on a Windows App and am making it single-instance app, that part I have working like this: static void Main() { try { System.Threading.Mutex mutex = new...
2
by: Lowell | last post by:
I want a (global) mutex which prevents a section of my code from being run by more than one process (or user) at a time. I have something that works, EXCEPT when there is an exception of some kind. ...
3
by: UJ | last post by:
Is there any way to forcibly destroy a mutex? Let's suppose I have a log file that I'm writing to. I have code in the write that creates a mutex. But some program somewhere hasn't released it...
8
by: DevNull | last post by:
Hello, I've searched and searched and cannot seem to figure it out so I was hoping someone could explain to me exactly what an unqualified-id is? My compiler is generating this error......
1
by: chsalvia | last post by:
I was browsing through the souce code for the boost mutex implementation just to understand how the library works. I get the basic idea: a mutex object is associated with a scoped_lock object....
167
by: darren | last post by:
Hi I have to write a multi-threaded program. I decided to take an OO approach to it. I had the idea to wrap up all of the thread functions in a mix-in class called Threadable. Then when an...
45
by: Chris Forone | last post by:
hello group, is there a chance for other functions to get the lock if i have following loop: while (running) { Lock local(mutex); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.