1

I'd like to use a running instance of my application (a single instance application) to run a new commandline... I've heard about mutexes and IPC mechanisms but I don't know how to use it.

Explanation :

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MsgBox(Environment.CommandLine)
    End Sub

End Class

Example :

I launch the app with a file as argument, it shows the MsgBox and I let it run. If I launch once again the app with a file as argument, it won't show the MsgBox... How can I show it with the new commandline ?

Regards, Drarig29.

Drarig29
  • 1,902
  • 1
  • 19
  • 42

1 Answers1

3

In VB.NET you can make your application single instance from the project properties page. Check the "Make single instance application" option, then click the "View Application Events" button:

Project Properties Page

In the ApplicationEvents.vb class, add a handler for StartupNextInstance - this will be called when the application is already running and you start it again. You can call a method on your main form:

Namespace My

    Partial Friend Class MyApplication

        Private Sub MyApplication_StartupNextInstance(sender As Object, e As ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            ' Handle arguments when app is already running
            If e.CommandLine.Count > 0 Then
                ' Pass the argument to the main form
                Dim form = TryCast(My.Application.MainForm, Form1)
                form.LoadFile(e.CommandLine(0))
            End If
        End Sub

    End Class

End Namespace

In your main form, you can pass the initial command line arguments, and handle the subsequent ones, with a common method:

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        ' Handle arguments from the initial launch
        Dim args = Environment.GetCommandLineArgs()
        If args.Length > 1 Then
            LoadFile(args(1))
        End If
    End Sub

    Public Sub LoadFile(filename As String)
        MessageBox.Show(filename)
    End Sub

End Class
Mark
  • 8,140
  • 1
  • 14
  • 29
  • Thank you for the answer, I will try this later... Could you upvote me ? I'd like to win some reputation... But just if you want ! ;) – Drarig29 Aug 03 '15 at 21:30
  • 1
    You should always try it out first before accepting an answer, so that future readers know if this solved your problem or not. There is no rush to accept an answer, and you might not remember to unaccept it later if it turns out not to help. :-) – Mark Aug 03 '15 at 21:39
  • But I don't really know what this line is used for : `Dim form = TryCast(My.Application.MainForm, Form1)`... I tried without this line and it works too... I replaced `form` by `Form1`, giving this line : `Form1.LoadFile(e.CommandLine(0))`. What do you think about this ? – Drarig29 Aug 03 '15 at 23:44
  • I've understood that it's to associate the object with the class file but, is it really useful given that it works without it ? – Drarig29 Aug 03 '15 at 23:51
  • 1
    `Form1` is the class name, so you would typically only use `Form1.LoadFile` if it is a `Shared` method. However. VB.NET ([but not C#](https://stackoverflow.com/questions/4698538/why-is-there-is-a-default-instance-of-every-form-in-vb-net-but-not-in-c)) makes a default instance of each form available using the class name, since that was how it worked in old versions of VB. That always seems a bit hacky to me, so I never use it. `My.Application.MainForm` give you a reference to the main form instance, which in this case is a `Form1` instance. Use which ever you prefer - same thing in the end. – Mark Aug 04 '15 at 02:43