使用参数启动 c# 程序的第二个实例,程序的第一个实例使用该参数
Posted
技术标签:
【中文标题】使用参数启动 c# 程序的第二个实例,程序的第一个实例使用该参数【英文标题】:Start 2nd instance of a c# program with a parameter, which the 1st instance of the program uses 【发布时间】:2011-02-10 12:51:17 【问题描述】:我有一个单实例程序,当它运行时我可以选择将单个参数传递给它。
如果程序在没有参数的情况下运行,只需在选项卡上打开一个 dataGridView 并将客户列表加载到其中。如果我双击一行,它会在第二个选项卡中打开该特定客户,其中包含更多信息。
如果我使用参数(从 00000 到 99999 的客户 ID 号)启动程序,它会自动切换到第二个选项卡并加载各个客户数据。
到目前为止一切都很好,但是,我想要做的是让我的程序运行,但是如果使用参数调用程序的第二个实例,例如Program.exe 1234,我希望它直接跳转到第二个选项卡并显示该客户的详细信息。
这是我目前所拥有的。我正在尝试这样做的方式是在吠叫错误的树吗?我的印象是 Program.exe 应该正在监听另一个正在运行的实例并使用传递给它的参数。
任何建议将不胜感激。
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())
//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;
【问题讨论】:
C# : how to - single instance application that accepts new parameters ? 的可能重复项 【参考方案1】:啊哈...找到了答案...有人叫 MadHatter 制作了一个 SingleInstance 应用程序,该应用程序完全符合我的要求,而且看起来相当简单。
http://sanity-free.org/misc/SingleInstanceApp.zip
【讨论】:
以上是关于使用参数启动 c# 程序的第二个实例,程序的第一个实例使用该参数的主要内容,如果未能解决你的问题,请参考以下文章
为啥对方法的第二个参数使用 await 运算符会影响第一个参数的值?