Russell Uhl
Russell Uhl

Reputation: 4581

c# pass cmd args to existing instance instead of new instance

I have a C# Windows Forms application that is manually started and then mostly runs in the background. In my registry, this program is set up to handle a "dstel" url protocol. That is, href="dstel:1234567890" will successfully open my program.

The issue is that my program needs have only one instance running at any given time. My research suggests that in order to ensure that multiple instances are not operating simultaneously, I should use a mutex (courtesy of How to check if another instance of the application is running).

Assuming that works, I am now left with what to do when someone clicks on my special dstel link in a webpage. What I need is how can I redirect the command line input to method xyz() of my already-running application?

To be clear, I'm envisioning the following events, in order:

  1. I manually start instance A of my program
  2. I click on the dstel link
  3. Instance B of my program starts
  4. Thanks to the mutex, instance B of my program realizes that instance A is already running
  5. Instance B passes its command line args to method xyz() of instance A. <<<< My issue
  6. Instance B exits

I have a few things in mind that might work, most notably either using a helper application or setting up a named pipe via WCF. I am open to any suggestions you may have.

Thanks!

Upvotes: 1

Views: 922

Answers (1)

jamesthollowell
jamesthollowell

Reputation: 1712

Look at http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.applicationservices.windowsformsapplicationbase(v=vs.110).aspx

If you override this class for your base app and set its protected IsSingleInstance property as true, you can override the method OnStartupNextInstance(), which gets called anytime a subsequent instance of your program is started.

This should do it like you want!

Upvotes: 1

Related Questions