启动单实例项目

时间:2013-07-20 09:05:22

标签: c# wpf

我希望我的应用程序只运行单个实例,我用于粘贴下的代码,但它给了我一个错误:

Error   1   'MYAPP.App' does not implement interface member 
            'Microsoft.Shell.ISingleInstanceApp.SignalExternalCommandLineArgs
             (System.Collections.Generic.IList<string>)'
            C:\Users\moataz\Desktop\MYAPP 26-06-2013\MYAPP\App.xaml.cs

http://www.codeproject.com/Articles/84270/WPF-Single-Instance-Application

4 个答案:

答案 0 :(得分:3)

正如您发布的文章所述 -

  

步骤3:让您的应用程序类实现ISingleInstanceApp   (在SingleInstance.cs中定义)。

因此,您需要为App.xaml.cs实现此界面。

虽然,我个人建议您使用Mutex来实现single instance application。可以找到详细信息here

答案 1 :(得分:0)

为什么不检查系统中正在运行的进程。如果您的应用程序名称在运行的进程中,请不要运行其应用程序的另一个实例。而是让该实例进行查看。

答案 2 :(得分:0)

在App.xaml.cs中添加以下代码,看看它是否适合您。

static EventWaitHandle s_event;
bool created;  

App()
{
    s_event = new EventWaitHandle (false, EventResetMode.ManualReset, "my program#startup", out created) ;
    if (!created)
    {
        if (MessageBoxResult.OK == MessageBox.Show("Only 1 instance of this application can run at a time", "Error", MessageBoxButton.OK))
            App.Current.Shutdown();
    }
}

答案 3 :(得分:0)

为什么不使用Mutex,你可以在 App.xaml.cs中执行此操作。

 Mutex myMutex ;

 private void Application_Startup(object sender, StartupEventArgs e)
 {
    bool aIsNewInstance = false;
    myMutex = new Mutex(true, "MyWPFApplication", out aIsNewInstance);  
       if (!aIsNewInstance)
        {
          MessageBox.Show("Already an instance is running...");
          App.Current.Shutdown();  
        }
  }