实例感知应用程序传递参数窗口窗体应用程序

Posted

技术标签:

【中文标题】实例感知应用程序传递参数窗口窗体应用程序【英文标题】:Instance aware application pass arguments window forms app 【发布时间】:2013-07-19 17:43:29 【问题描述】:

我想构建一个 c# 应用程序 - 它应该像我在网页中创建链接的方式一样,它应该像 Skype 那样执行“调用”操作给应用程序传递参数。我希望应用只在任务栏中。

所以我第一次启动应用程序,然后下次单击链接时它会在第一个应用程序上再次执行。

<a href="myapp:12345">Click me</a>

我将设置 Windows 注册表以做出相应的响应 那么它应该使用参数触发应用程序

例如myapp.exe /12345

我可以使应用程序运行,但每次单击链接时都会获得一个新实例,如何将参数传递给当前正在运行的应用程序?

【问题讨论】:

查看this 以防止出现第二个实例,对于传递参数,您必须在第一个应用程序中创建一个侦听器,在第二个应用程序中创建一个发送者。命名管道可以做到这一点,或者一个由一个读取并由另一个写入的文件。 如何将 args 传递给当前正在运行的应用程序? 好的,谢谢,我应该使用哪种类型的监听器? args = Environment.GetCommandLineArgs(); 我使用的是静态 void Main(string[] MyArgs) TCP 服务器是最简单的,因为它带有异步行为 【参考方案1】:

灵感来自 this 用于防止第二个实例和 this 用于命名管道服务器

static void Main(string[] args)


    string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();

    // unique id for global mutex - Global prefix means it is global to the machine
    string mutexId = string.Format("Global\\0", appGuid);

    using (var mutex = new Mutex(false, mutexId))
    

        var hasHandle = false;
        try
        
            try
            
                hasHandle = mutex.WaitOne(5000, false);
            
            catch (AbandonedMutexException)
            
                hasHandle = true;
            


            if (hasHandle)
            
                // main app
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Form1 frm = new Form1();

                HandleArgs(args,frm); // handle first start with args

                Server(appGuid, frm); // handle next clients

                Application.Run(frm);
            
            else
            
                //any next client..
                var cli = new NamedPipeClientStream(appGuid);
                // connect to first app
                cli.Connect();
                // serialiaze args and send over
                var bf = new BinaryFormatter();
                bf.Serialize(cli, args);  // the commandline args over the line
                cli.Flush();
                cli.Close();
                // done
            
        
        finally
        
            if (hasHandle)
                mutex.ReleaseMutex();
        
    


// do usefull stuff with the commandline args
static void HandleArgs(string[] args, Form1 frm)

    foreach (var s in args)
    
        // just append the args to the textbox
        frm.textBox1.Text += s;
        frm.textBox1.Text += Environment.NewLine;
    


// server that runs async
static void Server(string appGuid, Form1 frm)

    var srv = new NamedPipeServerStream(appGuid, PipeDirection.InOut, 5, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

    srv.BeginWaitForConnection(state =>
    
        NamedPipeServerStream nps = (NamedPipeServerStream)state.AsyncState;
        nps.EndWaitForConnection(state);

        var bf = new BinaryFormatter();
        string[] args = (string[])bf.Deserialize(nps);

        // don't forget to call on the UI thread
        frm.Invoke(new MethodInvoker(() =>  HandleArgs(args, frm); ));

        nps.Disconnect();

        Server(appGuid, frm); // restart server
    , srv);

【讨论】:

以上是关于实例感知应用程序传递参数窗口窗体应用程序的主要内容,如果未能解决你的问题,请参考以下文章

C# 窗体之间参数互相传递的两种方法与使用

如何将命名参数列表传递给打开的 Windows 窗体应用程序

VC++Windows窗体应用程序 窗体间如何传递变量

cmake3.18.0过程信息窗体怎么调出

在delphi中窗体间怎么传递参数

C#问题...Form2怎么获取Form1的按钮事件!..