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

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 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
3 4933
cold80 <se****************@gmail.comwrote:
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\\"+someUniqueName,
out firstInstance);

--
Jon Skeet - <sk***@pobox.com>
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.comwrote:
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.com>
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
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: 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...
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...
0
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...
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...
1
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...
6
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...
8
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.