473,770 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 WindowsApplicat ion2
Public Class Form1
Inherits Form

Public Sub New()

End Sub

Private Shared appGuid As String = "uniquekeyonmym achine"

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 WindowsApplicat ion1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}

static string appGuid = "uniquekeyonmym achine";

[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 2063
Hello cold80,

This works fine:

Imports System
Imports System.Threadin g
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.ReleaseM utex()
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 WindowsApplicat ion2
Public Class Form1
Inherits Form
Public Sub New()

End Sub

Private Shared appGuid As String = "uniquekeyonmym achine"

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 WindowsApplicat ion1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeCompo nent();
}
static string appGuid = "uniquekeyonmym achine";

[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
2777
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. I have not implemented any syncronization between reading and wrinting, just i checking size of queue, if it is not empty i am reading from queue and wrting to file. somethign like
2
4781
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 Mutex mtxBezoeken = new Mutex(false, "bezoeken"); which compiles and executes just fine. Then in another page I want to write to a file that may also be accessed by global.asax.cs so I want access to
4
11166
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 want to display a message (no problem here) and then abort the program. However, after the attached code executes I end up with my main form and a wait cursor! If I click on the X the form closes. Boss doesn't think an unhandled exception will...
1
1647
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 database to store 2 numbers, so I'm just trying to use a Mutex to be sure only one of my process at a time would access the file. my code looks like that: public static uint GetUID(string filename, byte keyType, byte reseller) { using (Mutex...
4
1641
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 not get a mutex lock. Any ideas? I'm trying to get a globally accessible mutex. Is using the Application object wrong somehow?
5
6046
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 ApplicationException with the explanation: "Object synchronisation method was called from an unsynchronised block of code". I'm sure this tells me exactly how to fix it... well, it would if I understood it. Can anyone suggest where I'm going wrong?...
3
4960
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 on visual basic. Actually, it works sometimes and sometimes not. The code I'm trying is: Namespace WindowsApplication2 Public Class Form1 Inherits Form
3
5594
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 the mutex in order to activate the thread once the data is generated. I have to do it this way, i can only call the thread if the data are generated. ******************************************************** step 1: initialize the mutex
3
6816
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 pattern. gcc-4.1.2 however complains with errors about my use of boost::bind(). Doesn't bind support this way of specifying a function and its arguments or should I solve it in another way?
0
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10101
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9906
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7456
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.