我有一个 WPF 应用程序,它可以采用一些可选的命令行参数。
这个应用程序也是一个单实例应用程序(如果一个实例已经打开,则使用互斥锁关闭任何实例)。
不过,我想要它做的是,如果某些东西试图用一些 cmd 行参数打开应用程序,那么应用程序将执行它应该对那些执行的操作(在我的应用程序中,它会根据 cmd 行打开不同的对话框)。
实现这一目标的最简单方法是什么?
在psedo代码中,这是我正在寻找的
protected override void OnStartup(StartupEventArgs e)
{
bool mutexIsNew;
using (System.Threading.Mutex m =
new System.Threading.Mutex(true, "MyApplication", out mutexIsNew))
{
//if this is not the first instance of the app
if (!mutexIsNew)
{
//if there is some cmd line args
if (e.Args.Length > 0)
{
//send the args to the older instance so it can handle them
SendToOtherInstance(e.Args);
//shutdown this new instance
Application.Current.Shutdown();
}
}
}
base.OnStartup(e);
}