Application.Restart with different command line arguments? (VS 2008)

casco32
casco32 used Ask the Experts™
on
Is it possible to restart a VB.NET application, but with different command line arguments than it originally had?

Here is the challenge. I want to write a single instance application. If the second instance has the same CL args as the first, then the built-in behavior is fine (second instance never opens, e.BringToForeground=True brings 1st instance to foreground, everything is great).

But if the second instance has different CL args, then I would like the first instance to close and the second instance to start. However, once you mark a VB.NET app as "single instance only", it appears that you can never get the second instance. I think: OK, fine, I can just restart my first instance from the StartupNextInstance event. But how do I use the new command line arguments from the second instance? The Restart() method does not take CL args that I can see.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
You could just change the "shutdown mode" to "when last form closes" (in Project --> Properties), then open a new instance of the "startup form" and close the original.  The args could be passed into the form or you could use Shared members in class.

Author

Commented:
Hi. Thanks, but I'd really rather restart the application with new arguments, rather than rearchitect it. You see, I actually have several applications that rather fit this odd requirement. If i find a way to restart them, I could use it with all of them. If I have to pseudo-restart them, then I'll have to redesign them all, making sure to clear any state and so forth. I'd rather avoid having to think through all of them.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Gotcha...I'll play with it today...
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
Alrighty...I've hacked something together...not sure if you can apply it to your situation:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MessageBox.Show(Environment.CommandLine) ' so you can see when Form1 has been reopened and what the new args are
    End Sub

End Class

Namespace My

    ' The following events are available for MyApplication:
    ' 
    ' Startup: Raised when the application starts, before the startup form is created.
    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.
    ' UnhandledException: Raised if the application encounters an unhandled exception.
    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 
    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.
    Partial Friend Class MyApplication

        Private FileName As String
        Private OrigArgs As New List(Of String)
        Private NewArgs As New List(Of String)

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
            FileName = Environment.GetCommandLineArgs(0)
            OrigArgs.Clear()
            OrigArgs.AddRange(e.CommandLine.ToArray)
        End Sub

        Private Sub MyApplication_StartupNextInstance(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            NewArgs.Clear()
            NewArgs.AddRange(e.CommandLine.ToArray)
            If NewArgs.Count = OrigArgs.Count Then
                For i As Integer = 0 To NewArgs.Count - 1
                    If NewArgs(i) <> OrigArgs(i) Then
                        Application.MainForm.Close() ' <-- assumes you have "shutdown mode" set to "when startup form closes"
                        Exit Sub
                    End If
                Next
                NewArgs.Clear() ' the arguments were the same, clear it out so the app will shut down properly
                e.BringToForeground = True
            Else
                Application.MainForm.Close() ' <-- assumes you have "shutdown mode" set to "when startup form closes"
            End If
        End Sub

        Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
            If NewArgs.Count > 0 Then
                Dim p As New Process
                p.StartInfo.FileName = FileName
                p.StartInfo.Arguments = String.Join(" ", NewArgs.ToArray)
                p.Start()
            End If
        End Sub

    End Class

End Namespace

Open in new window

Author

Commented:
Using the Shutdown event was the piece of the puzzle I was missing. I had been trying to restart from within StartupNextInstance. This works beautifully!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial