Click to See Complete Forum and Search --> : [RESOLVED] prevent instance of application
daimous
Oct 25th, 2006, 01:10 AM
hi guys!! is there anyway for me to prevent any other intance of my windows application to run? cause i can run 2 or more of my windows application which i dont want to happen. thans in advance guys!
jmcilhinney
Oct 25th, 2006, 01:22 AM
http://msdn.microsoft.com/msdnmag/issues/05/09/NETMatters/
daimous
Oct 25th, 2006, 03:07 AM
I come up the code below but still I can run another instance of my apps. btw, I call that method under Form_load. can you help me to figure out whats wrong...
public void chk_instance()
{
string mutexName = Assembly.GetEntryAssembly().FullName.ToString();
bool grantedOwnership;
Mutex SingleInstanceMutex = new Mutex(true, mutexName,out grantedOwnership);
if (! grantedOwnership)
{
MessageBox.Show("Another instance of this application is open", "BCMD", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
}
ComputerJy
Oct 25th, 2006, 05:02 AM
Try this
static bool running = false;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
if (!running)
{
running = true;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
Pasvorto
Oct 30th, 2006, 10:14 AM
I was under the impression that you could set the "single instance" switch in the project properties window.
ComputerJy
Oct 30th, 2006, 10:32 AM
I was under the impression that you could set the "single instance" switch in the project properties window.
That option exists only in VB2005
Pasvorto
Oct 30th, 2006, 10:40 AM
Oh, my bad.
daimous
Oct 30th, 2006, 05:50 PM
does VC#2005 has that option? i've tried to look at it but i haven't seen that option.
jmcilhinney
Oct 30th, 2006, 06:11 PM
I come up the code below but still I can run another instance of my apps. btw, I call that method under Form_load. can you help me to figure out whats wrong...
public void chk_instance()
{
string mutexName = Assembly.GetEntryAssembly().FullName.ToString();
bool grantedOwnership;
Mutex SingleInstanceMutex = new Mutex(true, mutexName,out grantedOwnership);
if (! grantedOwnership)
{
MessageBox.Show("Another instance of this application is open", "BCMD", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();
}
}
The link I posted specifically places the code to prevent multiple instances in the Main method.
jmcilhinney
Oct 30th, 2006, 06:21 PM
I created a new project, edited the Main method to the following and voila.using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace SingleInstanceTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
string mutexName = "3af65962-66bf-4135-bc5a-b0f986d8e995";
bool grantedOwnership;
System.Threading.Mutex singleInstanceMutex = new System.Threading.Mutex(true, mutexName, out grantedOwnership);
try
{
if (grantedOwnership)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
finally
{
singleInstanceMutex.Close();
}
}
}
}
daimous
Nov 8th, 2006, 05:43 PM
Sorry to ask JM but i dont have an idea where to place your code. Is static void Main() is automatically generated when i create a project or i need to hard-code it manually?
jmcilhinney
Nov 8th, 2006, 06:16 PM
Yes, the Main method is automatically generated. As you can see from the code it is a member of the Program class. The Program class is declared in the Program.cs code file.
daimous
Nov 16th, 2006, 11:59 PM
Thanks! it is working now.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.