Mutex Differences VB.NET & C#

  • Thread starter Thread starter John Bowman
  • Start date Start date
J

John Bowman

Hi,

I've got the following C# code that works perfectly to prevent a 2nd
instance of my C# app:

bool bAppNotRunning = false;
Mutex MutApp = new Mutex(true, "MyC#AppMutex", out bAppNotRunning)
if(bAppNotRunning == true)
{
Run the app and do work...
MutApp.ReleaseMutex();
}


I did the same thing in another app that's in VB. NET to prevent a 2nd
instace of it:

Dim bAppNotRunning As Boolean

bAppNotRunning = False
MutApp = New Mutex(True, "MyVBAppMutex", bAppNotRunning)
If(bApNotRunning = True) Then
Run the app and do work...
MutApp.ReleaseMutex()
End If


Why doesn't the VB version work? bAppNotRunning comes back True from the
Mutex constructor even when another instance of the app is already running?!

What gives? Any ideas would be appreciated.

TIA,
 
I've got the following C# code that works perfectly to prevent a 2nd
instance of my C# app:

bool bAppNotRunning = false;
Mutex MutApp = new Mutex(true, "MyC#AppMutex", out bAppNotRunning)
if(bAppNotRunning == true)
{
Run the app and do work...
MutApp.ReleaseMutex();
}

if ( bAppNotRunning ) ... forget about the " == true", because bAppRunning
is already a boolean value.

That's also the problem with the VB.NET-code:
Dim bAppNotRunning As Boolean

bAppNotRunning = False
MutApp = New Mutex(True, "MyVBAppMutex", bAppNotRunning)

-> in the next line, true is probably asssigned to bAppRunning because of
the braces and because "=" is in VB.NET used for both, assigning and
comparing.
If(bApNotRunning = True) Then


So ... if you check a boolean value, do it like this:

If (bAppNotRunning) Then ...

:-)

Regards,

Frank Eller
 
Back
Top