如何正确处理单实例应用程序? [英] How do I properly handling single instance applications?

查看:91
本文介绍了如何正确处理单实例应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何插入运行表单或将参数传递给所请求应用程序的现有实例的功能?例如,如果steam已经运行,则双击steam,然后运行的进程打开主窗体,同时请求的进程自行关闭。得到它?



  bool  createdNew; 
m_Mutex = new Mutex( true ,Application.ProductName, out createdNew);
if (createdNew){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
使用(ProcessIcon pi = new ProcessIcon()){
pi.Display ();
Application.Run();
}
} 其他
// 我知道我的问题与本节有关...


MessageBox.Show( 应用程序已在运行。,Application.ProductName,MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}

解决方案

确定流程是否已经运行非常简单:确保只有一个实例正在运行的简单方法。 [ ^ ]显示了我在代码中的表现。

它正在将数据传递给正在运行的实例,这就是问题所在。我要做的是在我的应用程序中设置一个Socket接收器,当我发现一个已经运行的时候,我会在关闭应用程序之前将数据发送到该Socket。然后,正在运行的应用程序可以使用数据执行所需操作。请参阅此处: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365574%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 [ ^ ]

How do I insert the ability to run a form or pass the arguments over to an existing instance of the requested application? For example, double click steam if steam is already running then the running process opens up the main form while the requested process closes itself out. Get it?

bool createdNew;
m_Mutex = new Mutex(true, Application.ProductName, out createdNew);
if (createdNew){
   Application.EnableVisualStyles();
   Application.SetCompatibleTextRenderingDefault(false);
   using (ProcessIcon pi = new ProcessIcon()){
       pi.Display();
       Application.Run();
   }
}else
    // I Know That My Question Pertains To This Section...


    MessageBox.Show("The application is already running.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}

解决方案

Finding out if the process is already running is pretty easy: A simple way to ensure only one instance is running.[^] shows how I do it in my code.
It's handing the data to the running instance that's the problem. What I would do is set up a Socket receiver in my app and when I found one already running I'd send the data to that Socket before I closed the app. The running app can then do what it wants with the data. See here: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365574%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396[^]


这篇关于如何正确处理单实例应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆