-4

I want that my application run only single instance, I used for that the code under pasted under, but It gives me an error :

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

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
tp1
  • 105
  • 8
  • Your question is completely different to your title, and your not implementing an interface correctly, right click the interface at top of your class and then click on implement – Sayse Jul 20 '13 at 09:10
  • Have you checked this SOF post ? It worked for me. http://stackoverflow.com/questions/646480/is-using-a-mutex-to-prevent-multipule-instances-of-the-same-program-from-running/646500 – Nitesh Jul 20 '13 at 09:11
  • @tp1 You should use Mutex for that. See my answer below on implementation. – Ehsan Jul 20 '13 at 09:51

4 Answers4

3

As stated in the article you posted -

Step 3: Have your application class implement ISingleInstanceApp (defined in SingleInstance.cs).

So, you need to implement this interface for your App.xaml.cs.

Though, personally i would suggest you to use Mutex to achieve single instance application. Detials can be found here.

Community
  • 1
  • 1
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
0

Why don't you check from running processes in system. If your application name is in running processes , don't run another instance of it your application. Instead bring that instance to view.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
0

Add below piece of code in your App.xaml.cs and see if it works for you.

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();
    }
}
Nitesh
  • 7,261
  • 3
  • 30
  • 25
0

why not use Mutex, You do this in 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();  
        }
  }
Ehsan
  • 31,833
  • 6
  • 56
  • 65