Command Line Parameters

G

Glen

I have a windows application that accepts command line parms at
startup. It works fine. However, if (while the app is running) the
user tries to run it again with another command line start, I would
like the existing instance of the app to take the new command line
parms and act appropriately.

Is there some event I can trap in my application that will tell me that
the user has tried to start this singleton app again and then get the
command line parms?

Thanks,
Glen
 
S

Stoitcho Goutsev \(100\)

Glen,

Nope, there is no such an event. You can discover from the new instance that
another one is already running - using mutex for example, but passing the
command line argumenst across the process boundaries is no that easy. For
transfering the arguments yous should use one of the interporcess
communication techniques - remoting, sockets or files (maybe there are
others, but these are supported by .NET that I came up with at the moment).
 
J

Jim Wooley

I have a windows application that accepts command line parms at
startup. It works fine. However, if (while the app is running) the
user tries to run it again with another command line start, I would
like the existing instance of the app to take the new command line
parms and act appropriately.

Is there some event I can trap in my application that will tell me
that the user has tried to start this singleton app again and then get
the command line parms?

There are a number of solutions out there. I have documented the methods
for .Net 1.x and VB.Net 2005 on my blog post at http://devauthority.com/blogs/jwooley/archive/2005/07/28/318.aspx.
It is trickier in 2003 as you need to set up a mutex and remoting channel
and then pass the commandline args from a new temporary instance through
the remoting channel to the running instance, make the running instance grab
focus and then kill the second temporary instance. You will need to watch
out for threading issues as this process sets up 4 threads that you need
to manage, and a form can not react to a call from a thread it was not created
under unless you take special measures.

In 2005, you set the application as a single instance application in the
project settings. Then in the Application Events, you handle the StartupNextInstance
event. The threading issues will magically go away as well.

Jim Wooley
http://devauthority.com/blogs/jwooley
 
O

Otis Mukinfus

There are a number of solutions out there. I have documented the methods
for .Net 1.x and VB.Net 2005 on my blog post at http://devauthority.com/blogs/jwooley/archive/2005/07/28/318.aspx.
It is trickier in 2003 as you need to set up a mutex and remoting channel
and then pass the commandline args from a new temporary instance through
the remoting channel to the running instance, make the running instance grab
focus and then kill the second temporary instance. You will need to watch
out for threading issues as this process sets up 4 threads that you need
to manage, and a form can not react to a call from a thread it was not created
under unless you take special measures.

In 2005, you set the application as a single instance application in the
project settings. Then in the Application Events, you handle the StartupNextInstance
event. The threading issues will magically go away as well.

Jim Wooley
http://devauthority.com/blogs/jwooley
Jim,

I don't find that setting in the project settings in the VS 2005 IDE. What am I
doing wrong?

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
J

Jim Wooley

There are a number of solutions out there. I have documented the
methods for .Net 1.x and VB.Net 2005 on my blog post at
http://devauthority.com/blogs/jwooley/archive/2005/07/28/318.aspx. It
is trickier in 2003 as you need to set up a mutex and remoting
channel and then pass the commandline args from a new temporary
instance through the remoting channel to the running instance, make
the running instance grab focus and then kill the second temporary
instance. You will need to watch out for threading issues as this
process sets up 4 threads that you need to manage, and a form can not
react to a call from a thread it was not created under unless you
take special measures.

In 2005, you set the application as a single instance application in
the project settings. Then in the Application Events, you handle the
StartupNextInstance event. The threading issues will magically go
away as well.

Jim Wooley
http://devauthority.com/blogs/jwooley
Jim,

I don't find that setting in the project settings in the VS 2005 IDE.
What am I doing wrong?[/QUOTE]

In the project dialog box, under Application , check the Enable Application
Framework box. Under that you should see "Make single instance application.
Check that. Then click the box for View application events. You can add the
event handler for the StartupNextInstance event and code it as necessary.
Here is my implementation:

Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal
e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs)
Handles Me.StartupNextInstance
If TypeOf Me.MainForm Is MdiMain Then
DirectCast(Me.MainForm, MdiMain).InstanceActivate(e)
End If
End Sub

My main form then has a method called InstanceActive as follows:
Friend Sub InstanceActivate(ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs)
If e.CommandLine.Count > 0 Then
For Each arg As String In e.CommandLine
'Process the commandline args
Next
End If
End Sub

I hope this helps.
Jim Wooley
http://devauthority.com/blogs/jwooley
 
O

Otis Mukinfus

In the project dialog box, under Application , check the Enable Application
Framework box. Under that you should see "Make single instance application.
Check that. Then click the box for View application events. You can add the
event handler for the StartupNextInstance event and code it as necessary.
Here is my implementation:

Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal
e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs)
Handles Me.StartupNextInstance
If TypeOf Me.MainForm Is MdiMain Then
DirectCast(Me.MainForm, MdiMain).InstanceActivate(e)
End If
End Sub

My main form then has a method called InstanceActive as follows:
Friend Sub InstanceActivate(ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs)
If e.CommandLine.Count > 0 Then
For Each arg As String In e.CommandLine
'Process the commandline args
Next
End If
End Sub

I hope this helps.
Jim Wooley
http://devauthority.com/blogs/jwooley
Bear with me Jim. When I choose Project/MyProjectName Properties... I don't
get a dialog box. I get a tabbed page with settings on it.

There is no Enable Application Framework checkbox on the Application tab. I'm
using VS 2005 Pro. Is what you described a an Enterprise feature?

Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
 
C

Claes Bergefall

In the project dialog box, under Application , check the Enable
Bear with me Jim. When I choose Project/MyProjectName Properties... I
don't
get a dialog box. I get a tabbed page with settings on it.

There is no Enable Application Framework checkbox on the Application tab.
I'm
using VS 2005 Pro. Is what you described a an Enterprise feature?

The settings that Jim refers to are only available for VB projects. Pro or
Enterprise version doesn't matter. Not sure what language you're using but
I'm guessing C# since you can't see them

/claes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top