473,785 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with named 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
3 4960
cold80 <se************ ****@gmail.comw rote:
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:
I suspect that the problem is that your mutex is being finalized while
your application is running, as others have suggested. The garbage
collector can finalize and release objects when they've last been used
- it doesn't have to wait until they "fall out of scope".

If you put either m.Dispose() or C.KeepAlive(m) fter the call to
Application.Run (new Form1()) I think you'll find that solves the
problem.

The call to Dispose is the equivalent to what the C# code is doing, by
the way (except that it does it in a finally block, effectively).

By the way, you don't need to call WaitOne. If you use the form of the
Mutex constructor which has a boolean out parameter as its last
parameter, you can get the information just from that. In C#:

bool firstInstance;
Mutex mutex = new Mutex(false, "Local\\"+someU niqueName,
out firstInstance);

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '06 #2
Thank you for your help, now it is working. Actually I read the same
article you are referring to and I was a little confused about the
solution they proposed. I thought that the GC couldn't touch my objects
before they went out of scope...but now you're saying that it can, so
the pieces are coming to the right place. But how can the GC know that
a specific object is not used anymore, so it can destroy it? Or the
destruction is inserted by the compiler whenever it realizes the
there's no more code that use a specific object? I would like to
understand this better before doing some trivial mistake...Do you have
some articles where I can refer to?

Thank you a lot

Cold

Sep 26 '06 #3
cold80 <se************ ****@gmail.comw rote:
Thank you for your help, now it is working. Actually I read the same
article you are referring to and I was a little confused about the
solution they proposed. I thought that the GC couldn't touch my objects
before they went out of scope...but now you're saying that it can, so
the pieces are coming to the right place. But how can the GC know that
a specific object is not used anymore, so it can destroy it? Or the
destruction is inserted by the compiler whenever it realizes the
there's no more code that use a specific object? I would like to
understand this better before doing some trivial mistake...Do you have
some articles where I can refer to?
No, the compiler doesn't tell anything to finalize/GC the object - it
can't tell whether or not it's in use somewhere else. What it *can* do
is mark the region where the *variable* is available. It knows the last
time the variable is read, and as far as the GC is concerned, the
variable doesn't exist after that point.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '06 #4

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

Similar topics

4
11167
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
1616
by: k a | last post by:
Hello NG I'm using a named mutex to protect shared data between 3 processes. I've noticed that each process that waits for the mutex uses 50% of CPU usage... Can someone tell me what to do to minimize the CPU usage in the waiting processes? thanks patrice
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?
0
1154
by: Dave | last post by:
I have two prorams, one of which runs as a service, and the uses a shared resource (actually a log file) which I want to protect with a named mutex. The service starts at login, and when I run the non-service program it throws an ApplicationException (Access denied) when it tries to construct the mutex. I can only assume that this is a security thing, maybe because the service is running under the Local System user. (If I stop the service,...
5
6049
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?...
1
2064
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
5595
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
6
9207
by: alexia.bee | last post by:
Hi all, I need to write app in C# that communicates with other app. I need to use "named event" for that. I should use windows APIs SetEvent() and WaitForSingleObject() etc... I looked for help but couldn't find any. Can any one please help me with that? Thanks.
8
574
by: Dan Pavel | last post by:
Hi, I did not used Mutex before and I need now. I have an application that control some workflows on different machines. I use SNMP for that. The problem is that the thread used for starting an workflow need to write in some OID. When I start 2 workflows on the same machine the second thread need to wait for the first one to finish and after that continue work. This works OK. The problem is when I have to start 2 workflows on different...
0
9491
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
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
10163
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
10104
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
8988
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
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
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.