|
-
Sep 11th, 2007, 01:33 AM
#1
Allowing only once instance of your application
This will only allow one instance of your application, i use this code for a couple of my programs/services...
Make sure you maker a reference to the System.Management class 
C# Code:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
int count = CheckForRunningInstances();
if (count <= 1)
{
Application.Run(new Form1());
}
else
{
System.Windows.Forms.MessageBox.Show("Another instance of this program is already running. Two instances cannot run at the same time", "Already running", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Application.Exit();
}
}
private static int CheckForRunningInstances()
{
string[] parts = System.Reflection.Assembly.GetExecutingAssembly().Location.Split("\\".ToCharArray());
string appName = parts[parts.Length - 1];
string query = "select name from CIM_Process where name = '" + appName + "'";
System.Management.ManagementObjectSearcher searcher = new System.Management.ManagementObjectSearcher(query);
int hInstCount = 0;
foreach (System.Management.ManagementObject item in searcher.Get())
{
hInstCount++;
if (hInstCount > 1) break;
}
return hInstCount;
}
Last edited by Paul M; Sep 11th, 2007 at 01:40 AM.
-
Nov 30th, 2007, 01:05 AM
#2
Re: Creating a Single Instance of your Application
kasracer,
Have you ever looked at any code that does this, but also can send any command line arguments from the second instance that is terminated, to the first instance that was already running?
It would exist in the "app already open, user double clicks file in windows that is associated with said app, about to be terminated instance needs to tell already open instance to handle the file" category.
I will go about figuring it out, as I have a need to do this, however I figured I would ask you first since you have this codebank entry.
In VB 2005, setting the "single instance application" checkbox accomplishes this well, however there is a nasty bug that has to do with that feature and firewall software that I need to try to eliminate. They use .NET remoting to accomplish this in VB 2005, which would not be an option to avoid the bug.
In 2008 you can used named pipe classes to do this, however the project it is for is not moving to a new framework anytime soon.
I have also seen some heavy API-centric methods of doing this that basically date back to VB6 but were ported to .NET before the whole single instance checkbox feature existed, however I was looking for a more managed code way first before I resort to direct calls to the Windows API.
-
Dec 24th, 2007, 12:48 PM
#3
Re: Creating a Single Instance of your Application
 Originally Posted by kleinma
Have you ever looked at any code that does this, but also can send any command line arguments from the second instance that is terminated, to the first instance that was already running?
It would exist in the "app already open, user double clicks file in windows that is associated with said app, about to be terminated instance needs to tell already open instance to handle the file" category.
I will go about figuring it out, as I have a need to do this, however I figured I would ask you first since you have this codebank entry.
I have not seen any code that can do that. I've looked into it but haven't found an easy way to do so. If you come up with a way or find a way that allows sending of commands to the already opened application without the use of APIs then please share
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
|