我想将应用程序新实例的命令行参数传递给已经运行的应用程序(如果存在)。到目前为止,我已经尝试了以下方法:
程序.cs
string[] Arguments = Environment.GetCommandLineArgs();
int iCurrentProcessID = -1;
using (Mutex mutex = new Mutex(true, Arguments[1], out createdNew)) //Finding if application is running or not
{
Process current = Process.GetCurrentProcess();
iCurrentProcessID = current.Id;
if (createdNew)
{
Application.Run(Frm1);
}
else
{
// Process current = Process.GetCurrentProcess();
Process CurrentAutomation = Process.GetProcessById(iCurrentProcessID);
string[] strArguments = Environment.GetCommandLineArgs();
if (!string.IsNullOrWhiteSpace(strArguments[2]))
{
frmMain.strEndtime = strArguments[2];
}
Form.cs
public partial class frmMain : Form
{
public static string strEndtime;
//...
}
值在第二个实例中设置正确,但在第一个(较早开始)实例中未设置。如何将这些值传递给应用程序的另一个实例?
据我所知,您可以利用 WCF 在进程之间发送值。您的主应用程序通过 net.pipe 公开 WCF 服务。检查这个link https://gorillacoding.wordpress.com/2013/02/03/using-wcf-for-inter-process-communication/欲了解详情
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)