我的主程序是这样实现的
void main(string args[])
{
...
}
假如我的最终编译文件是test.exe,他需要带一个命令参数例如"hello"
我运行test.exe hello是可以正常接收的
但我要再继续运行的话就又开启一个新进程了
而我是希望在已经运行的进程里继续接收新参数,例如我再运行text.exe bye时不是新开启一个进程而是使用已经启动的test程序

解决方案 »

  1.   

    [StructLayout( LayoutKind.Sequential)]
    public class SECURITY_ATTRIBUTES 
    {
    public int nLength; 
    public int lpSecurityDescriptor; 
    public int bInheritHandle; 
    } [System.Runtime.InteropServices.DllImport("kernel32")]
    private static extern int GetLastError();
    [System.Runtime.InteropServices.DllImport("kernel32")]
    private static extern IntPtr CreateMutex(SECURITY_ATTRIBUTES lpMutexAttributes,bool bInitialOwner,string lpName);
    [System.Runtime.InteropServices.DllImport("kernel32")]
    private static extern int ReleaseMutex(IntPtr hMutex);
    const int ERROR_ALREADY_EXISTS = 0183; [STAThread]
    static void Main() 
    {
    IntPtr hMutex;
    hMutex=CreateMutex(null,false,"test");
    if (GetLastError()!=ERROR_ALREADY_EXISTS)
    {
    Application.Run(new Form1());
    }
    else
    {
    MessageBox.Show("本程序只允许同时运行一个");
    ReleaseMutex(hMutex);
    }
    }