|
-
Oct 25th, 2006, 01:10 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] prevent instance of application
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!
-
Oct 25th, 2006, 01:22 AM
#2
Re: prevent instance of application
-
Oct 25th, 2006, 03:07 AM
#3
Thread Starter
Fanatic Member
Re: prevent instance of application
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...
Code:
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();
}
}
Last edited by daimous; Oct 25th, 2006 at 06:25 PM.
-
Oct 25th, 2006, 05:02 AM
#4
Re: prevent instance of application
Try this
Code:
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());
}
}
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 30th, 2006, 11:14 AM
#5
PowerPoster
Re: prevent instance of application
I was under the impression that you could set the "single instance" switch in the project properties window.
-
Oct 30th, 2006, 11:32 AM
#6
Re: prevent instance of application
 Originally Posted by Pasvorto
I was under the impression that you could set the "single instance" switch in the project properties window.
That option exists only in VB2005
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Oct 30th, 2006, 11:40 AM
#7
PowerPoster
Re: prevent instance of application
-
Oct 30th, 2006, 06:50 PM
#8
Thread Starter
Fanatic Member
Re: prevent instance of application
does VC#2005 has that option? i've tried to look at it but i haven't seen that option.
Last edited by daimous; Oct 30th, 2006 at 07:01 PM.
-
Oct 30th, 2006, 07:11 PM
#9
Re: prevent instance of application
 Originally Posted by daimous
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...
Code:
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.
-
Oct 30th, 2006, 07:21 PM
#10
Re: prevent instance of application
I created a new project, edited the Main method to the following and voila.
Code:
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();
}
}
}
}
-
Nov 8th, 2006, 06:43 PM
#11
Thread Starter
Fanatic Member
Re: prevent instance of application
Sorry to ask JM but i dont have an idea where to place your code. Is is automatically generated when i create a project or i need to hard-code it manually?
-
Nov 8th, 2006, 07:16 PM
#12
Re: prevent instance of application
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.
-
Nov 17th, 2006, 12:59 AM
#13
Thread Starter
Fanatic Member
Re: prevent instance of application
Thanks! it is working now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|