473,403 Members | 2,071 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,403 software developers and data experts.

Problem with mutex

I'm trying to check in my application if another instance of it is
already running. I found many code snippets on the net that make use of
a named mutex to do this check, but I can't make it work on visual
basic. Actually, it works sometimes and sometimes not. The code I'm
trying is:

Namespace WindowsApplication2
Public Class Form1
Inherits Form

Public Sub New()

End Sub

Private Shared appGuid As String = "uniquekeyonmymachine"

Public Shared Sub Main()
Dim m As Mutex

m = New Mutex(False, appGuid)
If m.WaitOne(0, False) = False Then
MessageBox.Show("Instance already running")
Return
End If

Application.Run(New Form1())
End Sub
End Class
End Namespace

Using this different piece of code in C# (and the keyword "using") the
method seems to work without problems (and always)

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

static string appGuid = "uniquekeyonmymachine";

[STAThread]
static void Main()
{
using (Mutex mutex = new Mutex(false, appGuid))
{
if (!mutex.WaitOne(0, false))
{
MessageBox.Show("Instance already running");
return;
}

Application.Run(new Form1());
}

}
}
}

What do you think can be the problem? Some people suggested that the
Garbage Collector could have destroy the mutex object before I tryied
to run another instance of the program, but the call to Application.Run
is a blocking one, isn't it? So the local object m should live until
the end of the program, right?

Thank you in advance for your help.

Cold

Sep 26 '06 #1
1 2042
Hello cold80,

This works fine:

Imports System
Imports System.Threading
Module Module1

Public Sub Main()

Dim tMutex As Mutex = New Mutex(False, "oggieboogieboo")
If Not tMutex.WaitOne(0, False) Then
MsgBox("This is a single-instance app, chedarhead.")
Return
End If

Application.Run(New Form1)
tMutex.ReleaseMutex()
tMutex = Nothing

End Sub

End Module

Also, be aware in VB you must set the startup form to "SUb Main()" in the
project properties. In VS05 this means unchecking the "Use Application Framework"
box first.

-Boo
I'm trying to check in my application if another instance of it is
already running. I found many code snippets on the net that make use
of a named mutex to do this check, but I can't make it work on visual
basic. Actually, it works sometimes and sometimes not. The code I'm
trying is:

Namespace WindowsApplication2
Public Class Form1
Inherits Form
Public Sub New()

End Sub

Private Shared appGuid As String = "uniquekeyonmymachine"

Public Shared Sub Main()
Dim m As Mutex
m = New Mutex(False, appGuid)
If m.WaitOne(0, False) = False Then
MessageBox.Show("Instance already running")
Return
End If
Application.Run(New Form1())
End Sub
End Class
End Namespace
Using this different piece of code in C# (and the keyword "using") the
method seems to work without problems (and always)

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static string appGuid = "uniquekeyonmymachine";

[STAThread]
static void Main()
{
using (Mutex mutex = new Mutex(false, appGuid))
{
if (!mutex.WaitOne(0, false))
{
MessageBox.Show("Instance already running");
return;
}
Application.Run(new Form1());
}
}
}
}
What do you think can be the problem? Some people suggested that the
Garbage Collector could have destroy the mutex object before I tryied
to run another instance of the program, but the call to
Application.Run is a blocking one, isn't it? So the local object m
should live until the end of the program, right?

Thank you in advance for your help.

Cold

Sep 26 '06 #2

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

Similar topics

4
by: Sachin Jagtap | last post by:
Hi, I am getting exception while poping item from queue, I am writing messages coming from client in a queue in one thread and then in other thread i am reading from queue and writing in file....
2
by: Martin Maat | last post by:
Hi. I want to use the same mutex in different classes (web pages in an ASP.NET application). In global.asax.cs, the class that starts up first, I create a Mutex like this: static private...
4
by: Chuck | last post by:
Hello everybody, I need to abort execution during start up, while the constructor called by Application.Run is executing. If the database fails to connect during my application's startup I...
1
by: Lloyd Dupont | last post by:
I have some code where I updatde a file (that might not exist yet). In fact this file contains 2 integers and is used in only one function. I thought overkill to configure, deploy and maintain a...
4
by: google | last post by:
Hi Guys, I'm having a bizarre problem here with mutexes. If I have the following code in a page (note that it never releases the mutex!), then load the page twice, in 2 seperate browsers, I do...
5
by: Steve Barnett | last post by:
I've tried to create a "single instance" application using a mutex and, as far as I can see, it's functioning right up until I close the application. When I try to release the mutex, I get an...
3
by: cold80 | last post by:
I'm trying to check in my application if another instance of it is already running. I found many code snippets on the net that make use of a named mutex to do this check, but I can't make it work...
3
by: NaeiKinDus | last post by:
Hello, i'm trying to program a thread that would be locked (by a mutex) and that would only be unlocked once that a function (generating data) is done. The purpose is to generate data, and unlock...
3
by: =?iso-8859-1?B?Tm9yZGz2dw==?= | last post by:
Hey there, C++ Coders! I am learning multi-threading with boost and have come up with the following code example shown below. This example implements a test of the producer-consumer design...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.