NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Process.Start (String)

Starts a process resource by specifying the name of a document or application file. Associates the process resource with a new Process component.

[Visual Basic]
Overloads Public Shared Function Start( _
   ByVal fileName As String _
) As Process
[C#]
public static Process Start(
   string fileName
);
[C++]
public: static Process* Start(
   String* fileName
);
[JScript]
public static function Start(
   fileName : String
) : Process;

Parameters

fileName
The name of a document or application file to run in the process.

Return Value

A new Process component associated with the process resource, or a null reference (in Visual Basic Nothing) if no process resource is started (for example, if an existing process is reused).

Exceptions

Exception Type Condition
ArgumentException The fileName parameter is a null reference (Nothing).

Remarks

Use this overload to start a process resource by specifying its file name, and associate it with a new Process component. If the process is already running, no additional process resource is started. Instead, the existing process resource is reused and no new Process component is created. In such a case, instead of returning a new Process component, the call to Start returns a null reference (Nothing) to the calling procedure.

This overload lets you start a process without first instantiating a Process object. It is an alternate for the explicit steps of instantiating a Process object, setting the FileName member of the StartInfo property, and calling Start on the Process.

The behavior of a process started by specifying its fileName is similar to typing the fileName into the Run dialog box of the Windows Start menu. Therefore, the fileName does not have to be an executable. It can be any file type whose extension has been associated with an application installed on the system, for example ".txt" if you have associated text files with an editor like Notepad, or ".doc" if you have .doc files associated with a word processing tool like Microsoft Word. Similarly, in the same way that the Run dialog box can accept an executable name with or without the extension, the extension is optional as well in the fileName parameter. For example, you can set the fileName to either "Notepad.exe" or "Notepad".

This overload of the Start method is a static (in Visual Basic Shared) member. You do not need to create an instance of the Process component before you call the method. You can call Start on the Process class itself. The process resource is automatically associated with the new Process component returned by the call to the Start method.

Unlike the other overloads, the parameterless overload of Start is not a static (Shared) member. Use the parameterless overload when you have already created a Process instance, specified start information including the file name, and want to start a process resource and associate it with the existing Process. Use one of the static (Shared) overloads when you want to create a new Process component rather than start a process on an existing component. Both this overload and the parameterless overload allow you to specify the file name of the process resource to start.

Whenever you use Start to start a process, you must be sure to close it or you risk losing system resources. You close processes using CloseMainWindow or Kill. CloseMainWindow is used to close processes that contain a graphical interface, while Kill must be used for non-graphical applications that do not have a message pump to handle a windows Close request.

Note   Currently, all processes started using a Process component open in a command window. They contain a message loop and you can attempt to close them using CloseMainWindow. This will change in a future release. Depending on how the non-graphical interface process is coded, CloseMainWindow could fail and Kill could be necessary in order to stop the process.

Example [Visual Basic]

The following example demonstrates opening Notepad by passing the name of the application to the Start method. Because Notepad could receive a close request before the window has opened and initialized, the procedure instructs it to wait until Notepad is in its message loop in an idle state before receiving input. The example code results in the opening a blank Notepad window.

[Visual Basic]

Private Sub StartWithFileName()
    'Declare and instantiate a new process component.
    Dim myproc As System.Diagnostics.Process
    myproc = New System.Diagnostics.Process

    'Do not receive an event when the process exits.
    myproc.WatchForExit = False
    'Start Notepad and assign it to the process component.    
    myproc.Start "Notepad"

    'This prevents the window from accepting a close before it has fully opened.
    myproc.WaitForInputIdle
End Sub

See Also

Process Class | Process Members | System.Diagnostics Namespace | Process.Start Overload List | FileName | StartInfo | CloseMainWindow | Kill