Use the Process class running program

xiaoxiao2021-03-06  131

Only SHELLEXECUTE can be ensured in the STA thread. In the implementation of Process, this problem is not taken into account, so running shellexecute using the Process class may be wrong. If you can't guarantee the ApartmentState of calling Process.start, you can use the following code to avoid this question:

Using system;

Using system.threading;

Public class foo {

Public static void OpenURL () {

System.Diagnostics.Process.start (@ "http://www.google.com");

}

Public static void

Main

() {

ThreadStart OpenURLDELEGATE = New ThreadStart (foo.openurl);

Thread mythread = new thread (OpenURLDELEGATE);

Mythread.SetaPartmentState (ApartmentState.sta);

mythread.start ();

mythread.join ();

}

}

When using the Process class to open unknown type files, the error code is Win32Exception for ERROR_NO_ASSOCIATION.

Set ErrorDialog in ProcessStartInfo to display the "Open WITH" dialog:

Using system.diagnostics;

Class test {

Public static void

Main

() {

ProcessStartInfo Info = New ProcessStartInfo ();

Info.filename = "t.tt";

INFO.ERRORDIALOG = TRUE;

Process.start (Info);

}

}

转载请注明原文地址:https://www.9cbs.com/read-127044.html

New Post(0)