1

With this code I can deserialize created files with my app:

[STAThread]
static void Main(string[] args)
{
     var application = new App();
     application.InitializeComponent();
     application.Run(args.Length == 0 ? new MainWindow() : new MainWindow(args[0]));
     //When passing a argument in mw constructor the deserialization will be called
}

My problem is that when the app is already opened, I'll get a new instance of my app. How can I detect whether it is still opened that my file will be opened in the right instance?

Thanks!

  • [Does this answer your question?](https://stackoverflow.com/a/94255/12888024) – aepot Aug 04 '20 at 19:01
  • Not sure I understand your request. Is it fine to have more than one instance open? If so, you want to make sure the file is opened in the new instance only and not the existing instance's? If you only want one instance open, you want to make sure that file is opened/read through the existing instance? – Trevor Aug 04 '20 at 19:03
  • To clearify: Its ok to have more than one instance of the app but when one instance is already opened, it should open the file in this instance and not start a new one. With a mutex I could catch it, that only one instance is possible but how can I control it when opening a file, in which instance it opens. – user23623447 Aug 04 '20 at 19:55
  • Same thing: Use a `Mutex` to control access to the file. If that `Mutex` is already owned, you know the file is opened in another instance. You could also open it with a lock on read/write, so if another instance tried to open it, it would fail. – Andy Aug 04 '20 at 23:46
  • @Andy Thanks! The detection whether the file is opened in another instance is solved. But then when it's owned I've to say the other instance that it should trigger the command in the first instance that the file should open there. How can I trigger commands across instances? – user23623447 Aug 06 '20 at 17:23
  • @user23623447 -- take a look at `WM_COPYDATA` -- you could start here: https://www.codeproject.com/Articles/1224031/Passing-Parameters-to-a-Running-Application-in-WPF – Andy Aug 06 '20 at 17:32

0 Answers0