WindowsFormsApplicationBase.StartupNextInstance Event

Note: This event is new in the .NET Framework version 2.0.

Occurs when attempting to start a single-instance application and the application is already active.

Namespace: Microsoft.VisualBasic.ApplicationServices
Assembly: Microsoft.VisualBasic (in microsoft.visualbasic.dll)

public event StartupNextInstanceEventHandler StartupNextInstance
/** @event */
public void add_StartupNextInstance (StartupNextInstanceEventHandler obj)

/** @event */
public void remove_StartupNextInstance (StartupNextInstanceEventHandler obj)

JScript supports the use of events, but not the declaration of new ones.

For more detailed information, see the Visual Basic topic My.Application.StartupNextInstance Event.

A single-instance application raises the StartupNextInstance event when you attempt to restart the application when it is already active. When a single-instance application starts for the first time, it raises the Startup event. For more information, see My.Application.Startup Event and How to: Specify Instancing Behavior for an Application.

This event is part of the Visual Basic Application model. For more information, see Overview of the Visual Basic Application Model.

This event is raised on the application's main thread with the other user-interface events. This allows the event handler to access directly the application's user interface. However, if the application is busy handling another user-interface event when this event is raised, this event cannot be processed until the other event handler finishes or calls the My.Application.DoEvents Method.

NoteNote

The StartupNextInstance event is raised only in single-instance applications. To enable single-instance behavior for your application, you must check the Make single instance application check box in the Project Designer.

To make an application single instance

With a project selected in Solution Explorer, click Properties on the Project menu.

Click the Application tab.

Select the Make single instance application check box.

For more information, see How to: Specify Instancing Behavior for an Application.

You must use the CommandLine property of the e parameter to access the arguments for subsequent attempts to start a single-instance application. The My.Application.CommandLineArgs Property provides the arguments used to start the first instance of a single-instance application.

The code for the StartupNextInstance event handler is stored in the ApplicationEvents.vb file, which is hidden by default.

To access the Code Editor window for application events

With a project selected in Solution Explorer, click Properties on the Project menu.

Click the Application tab.

Click the View Application Events button to open the Code Editor.

For more information, see How to: Handle Application Events (Visual Basic).

The following table lists examples of tasks involving the My.Application.StartupNextInstance event.

To

See

Use the events provided by the Visual Basic Application model to run code

How to: Run Code When the Application Starts or Ends

Check the command-line arguments of the first application instance

My.Application.CommandLineArgs Property

This example uses the e parameter of the StartupNextInstance event handler to examine the application's command-line arguments. If an argument is found that starts with /input=, the rest of that argument is displayed.

Private Sub MyApplication_StartupNextInstance( _
    ByVal sender As Object, _
    ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs _
) Handles Me.StartupNextInstance
    Dim inputArgument As String = "/input="
    Dim inputName As String = ""

    For Each s As String In e.CommandLine
        If s.ToLower.StartsWith(inputArgument) Then
            inputName = s.Remove(0, inputArgument.Length)
        End If
    Next

    If inputName = "" Then
        MsgBox("No input name")
    Else
        MsgBox("Input name: " & inputName)
    End If
End Sub

You must enter the code in the Code Editor window for application events. To access this window, follow the instructions from this topic's Remarks section. For more information, see How to: Handle Application Events (Visual Basic).

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0

Community Additions

ADD
Show: