Passing the command line to the first instance of a single instance application

I have already implemented the context menu that appears when the user right-clicks a file in Windows Explorer using Registry. The file address will be transferred to the application in the form of command lines. Clarity is not a problem.

How can I implement this similar to "Add to Windows Media Player Playlist"? It does not open another instance of the application, but works in the same open window and adds it to the list?

+4
source share
2 answers

There are two ways to do this, depending on how your application starts.

1. VB

, . :

Public Class MyMainForm       ' note the class name of the form
    ...

    Public Sub NewArgumentsReceived(args As String())
        ' e.g. add them to a list box
        If args.Length > 0 Then
            lbArgs.Items.AddRange(args)
        End If
    End Sub

:

  • " "
  • , ; MyApplication Events ; StartupNextInstance .

, :

Private Sub MyApplication_StartupNextInstance(sender As Object,
                e As ApplicationServices.StartupNextInstanceEventArgs) _
                     Handles Me.StartupNextInstance

    Dim f = Application.MainForm
    '  use YOUR actual form class name:
    If f.GetType Is GetType(MyMainForm) Then
        CType(f, MyMainForm).NewArgumentsReceived(e.CommandLine.ToArray)
    End If

End Sub

. Application.OpenForms. , . Application.MainForm .

- , ( , ).


2: Sub Main

, Sub Main , VB Application Framework , StartupNextInstance. WindowsFormsApplicationBase, .

- NewArgumentsReceived(args As String()), .

, , Sub Main():

  • "" .
  • Public Sub Main().
  • → →
  • Enable Application Framework
  • "Sub Main" Startup Object

, Program - , VS #. Sub Main . MSDN - .

Imports Microsoft.VisualBasic.ApplicationServices
Imports System.Collections.ObjectModel

Public Class SingleInstanceApp
    ' this is My.Application
    Inherits WindowsFormsApplicationBase

    Public Sub New(mode As AuthenticationMode)
        MyBase.New(mode)
        InitializeApp()
    End Sub

    Public Sub New()
        InitializeApp()
    End Sub

    ' standard startup procedures we want to implement
    Protected Overridable Sub InitializeApp()
        Me.IsSingleInstance = True
        Me.EnableVisualStyles = True
    End Sub

    ' ie Application.Run(frm):
    Public Overloads Sub Run(frm As Form)
        ' set mainform to be used as message pump
        Me.MainForm = frm
        ' pass the commandline
        Me.Run(Me.CommandLineArgs)
    End Sub

    Private Overloads Sub Run(args As ReadOnlyCollection(Of String))
        ' convert RO collection to simple array
        ' these will be handled by Sub Main for the First instance
        '    and in the StartupNextInstance handler for the others
        Me.Run(myArgs.ToArray)
    End Sub

    ' optional: save settings on exit
    Protected Overrides Sub OnShutdown()

        If My.Settings.Properties.Count > 0 Then
            My.Settings.Save()
        End If

        MyBase.OnShutdown()
    End Sub
End Class

, , App Framework ( " XP", " " " " ), . Sub Main:

Imports Microsoft.VisualBasic.ApplicationServices
Imports System.Collections.ObjectModel

Module Program  
    ' this app main form
    Friend myForm As MyMainForm

    Public Sub Main(args As String())
        ' create app object hardwired to SingleInstance
        Dim app As New SingleInstanceApp()

        ' add a handler for when a second instance tries to start
        ' (magic happens there)
        AddHandler app.StartupNextInstance, AddressOf StartupNextInstance

        myForm = New MyMainForm

        ' process command line args here for the first instance
        ' calling the method you added to the form:
        myForm.NewArgumentsReceived(args)

        ' start app
        app.Run(myForm)   
    End Sub

    ' This is invoked when subsequent instances try to start.
    '    grab and process their command line 
    Private Sub StartupNextInstance(sender As Object, 
                        e As StartupNextInstanceEventArgs)

        ' ToDo: Process the command line provided in e.CommandLine.
        myForm.NewArgumentsReceived(e.CommandLine.ToArray)

    End Sub   

End Module

SingleInstanceApp Sub Main, -, , , NewArgumentsReceived.


, , , . :

C:\Temp > singleinstance "First Inst"

, . :

C:\Temp > singleinstance "Next Inst" ziggy zoey zacky
  C:\Temp > singleinstance "Last Inst" 111 222 3333

, - . :

enter image description hereenter image description here

, , . , . , , , - .

+9

@Plutonix .

, , .. , , , , Application.MainForm ( ).

- , . NewArguments , , .

2 Plutonix:

1) Application.MainForm ( , Application.MainForm /, ).

2) , MainForm:

:

Public Interface INewArgumentsReceived
    Sub NewArgumentsReceived(args As String())
End Interface

@Plutonix MyApplication_StartupNextInstance :

Private Sub MyApplication_StartupNextInstance(sender As Object, e As ApplicationServices.StartupNextInstanceEventArgs) Handles Me.StartupNextInstance
            Dim f = Application.MainForm

            If f.GetType.GetInterfaces.Contains(GetType(INewArgumentsReceived)) Then
                CType(f, INewArgumentsReceived).NewArgumentsReceived(e.CommandLine.ToArray)
            Else
                MsgBox("The current program state can't receive new requests.",, vbExclamation)
            End If

, , INewArgumentsReceived:

Public Class FormA: Implements INewArgumentsReceived

    Public Sub NewArgumentsReceived(args As String()) Implements INewArgumentsReceived.NewArgumentsReceived
         MsgBox("Got new arguments")
    End Sub

, , Application.MainForm .

Application.MainForm , .

0

All Articles