[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

URGENT: Pass cmd line or other messages to active instance of application

I have an app that only allows one instance to be running at a time.  I'm currently using ShowWindow & SetForegroundWindow in the Main entry point to detect the ealier process and then activate it (via the handle, etc) when found & then the 2nd instance shuts down.  This all works well for a very long time now.

However, I now need to modify the load to accept command line args & also files (via a file association).  Right now, everything works fine when there is no other instance open - both the cmd line args and/or the file when double clicked open the application, process the args, etc.  My question is - if in the state where the app is already open, how do I pass the file info or other generic cmd line data to the first already opened instance before shutting the 2nd instance down?

Can this be done easily via SendNotifyMessage, or the like?  Does anyone have any sample C# code that would do this.  I'm using .NET 2.0 if that helps w/ a more elegant solution.

Thanks in advance.
0
slapiwite
Asked:
slapiwite
  • 2
  • 2
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"I'm using .NET 2.0 if that helps w/ a more elegant solution."

.Net 2.0 has a new "Application Model" that is designed to do just as you are describing.

Take a look at the StartupNextInstance() event:
http://msdn2.microsoft.com/en-US/library/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.startupnextinstance.aspx

I haven't tried using it with C# before...

0
 
slapiwiteAuthor Commented:
Hmm.. looks interesting.  There doesn't appear to be a C# equivalent.  However, I suppose I could always add a reference to Microsoft.VisualBasic & then modify how my app is loaded using this new model.

Or, if I can use the VB design to create a similar 'Application Model' for C#...

I'll leave htis question open while I play with the two above & see if it does the trick in case anyone else has any ideas that are C# oriented.  Otherwise, all the points are yours - thanks for the help!
0
 
topdog770Commented:
There is a C# approach for this... let me see if I can find the example..
0
 
topdog770Commented:
From < http://www.codeproject.com/csharp/CSSIApp.asp >

Introduction
A single-instance application is one that allows the user to open only one instance at a time. Strictly speaking, a second instance is started, but it detects the presence of the first instance, and shuts itself down.

Visual Studio 2005 makes single-instance applications very easy. In VB, one simply checks a box on the project settings dialog, and then handles the appropriate events. The code that accomplishes this is in the Microsoft.VisualBasic namespace, but it can be used in a C# application. This article will show you how to do this, and show how to transfer command-line arguments from the second instance to the original one.

// the link has details and source code

0
 
slapiwiteAuthor Commented:
I've split the comments - they were both helpful in finding a solution.

Thanks to both of you.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now