Start 2nd instance of a c# program with a parameter, which the 1st instance of the program uses
I have a single instance program, which when it runs I can choose to pass a single parameter to it.
If the program runs with no parameters, just opens up on a tab a dataGridView and loads a list of customers into it. If I double click a row it opens up that particular customer in a 2nd tab with more info on it.
If I start the program with a parameter, (a customer id number from 00000 to 99999) it automatically switches to the 2nd tab and loads that individual customers data.
So far so good, however, What I want to be able to do is have my program running, but if a 2nd instance of the program is called with a parameter, e.g. Program.exe 1234, I want it to just jump straight to the 2nd tab and display that customer's details.
This is what I have so far. Am I barking up the wrong tree with the way I'm trying to do this? I get the impression the Program.exe should be listening for another instance running and using the parameter passed to it.
Any advice would be greatly appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Reflection;
using System.IO;
namespace SiteConnex
{
public class SingleApplication
{
/// Imports
[DllImport("user32.dll")]
private static extern int ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
private static extern int SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern int IsIconic(IntPtr hWnd);
public static bool secondInstance = false;
public static string siteid = "";
public static bool gotosite = false;
public static void Main(string[] args)
{
// string siteid = "";
// bool gotosite = false;
// bool secondInstance = false;
int testSiteid = 0;
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
siteid = Convert.ToString(args[0]);
}
catch (Exception noArgs)
{
// No commandline args have been passed.
}
// check that the parameter passed is actually a valid number, otherwise just act like no params passed.
try
{
testSiteid = Convert.ToInt32(siteid);
if ((testSiteid > 00000) || (testSiteid < 99999))
{
gotosite = true;
}
else
{
siteid = "";
}
}
catch (Exception e)
{
// If you get an exception it means siteid had duff data passed into it.
}
/// Check if it's running, if it is, pass siteid and gotosite through... and acts as if it has a valid siteid passed to it.
// Application.Run(new Form1(siteid, gotosite));
Run(siteid, gotosite, secondInstance);
}
private static IntPtr GetCurrentInstanceWindowHandle()
{
IntPtr hWnd = IntPtr.Zero;
Process process = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(process.ProcessName);
foreach(Process _process in processes)
{
// Get the first instance that is not this instance, has the
// same process name and was started from the same file name
// and location. Also check that the process has a valid
// window handle in this session to filter out other user's
// processes.
if (_process.Id != process.Id &&
_process.MainModule.FileName == process.MainModule.FileName &&
_process.MainWindowHandle != IntPtr.Zero)
{
hWnd = _process.MainWindowHandle;
break;
}
}
return hWnd;
}
/// SwitchToCurrentInstance
private static void SwitchToCurrentInstance(string siteid, bool gotosite, bool secondInstance)
{
IntPtr hWnd = GetCurrentInstanceWindowHandle();
if (hWnd != IntPtr.Zero)
{
// Restore window if minimised. Do not restore if already in
// normal or maximised window state, since we don't want to
// change the current state of the window.
if (IsIconic(hWnd) != 0)
{
ShowWindow(hWnd, SW_RESTORE);
}
// Set foreground window.
SetForegroundWindow(hWnd);
secondInstance = true;
}
}
/// Execute a form base application if another instance already running on
/// the system activate previous one
/// <param name="frmMain">main form</param>
/// true if no previous instance is running
// public static bool Run(System.Windows.Forms.Form frmMain)
public static bool Run(string siteid, bool gotosite, bool secondInstance)
{
if(IsAlreadyRunning())
{
开发者_C百科 //set focus on previously running app
SwitchToCurrentInstance(siteid, gotosite, secondInstance);
return false;
}
Application.Run(new Form1(siteid, gotosite, secondInstance));
return true;
}
/// check if given exe alread running or not
/// returns true if already running
private static bool IsAlreadyRunning()
{
string strLoc = Assembly.GetExecutingAssembly().Location;
FileSystemInfo fileInfo = new FileInfo(strLoc);
string sExeName = fileInfo.Name;
bool bCreatedNew;
mutex = new Mutex(true, "Global\\"+sExeName, out bCreatedNew);
if (bCreatedNew)
mutex.ReleaseMutex();
return !bCreatedNew;
}
static Mutex mutex;
const int SW_RESTORE = 9;
}
}
Ah ha... found the answer in this... Someone called MadHatter made an SingleInstance app that does exactly what I was looking to do and seems fairly straight forwards.
http://sanity-free.org/misc/SingleInstanceApp.zip
精彩评论