In C #, the current Process of the operating system can be operated. The Process class provides access to processes running on the computer. Here we will discuss a confusing concept, Process and thread. simply put, a process is the application currently running on the computer, and a thread is the basic unit for the operating system to allocate processor time to the process. the system process is uniquely identified by its process identifier. however, in Windows, the process is identified by its handle, and the handle may not be unique on the computer. Even if the process has exited, the operating system still maintains the Process Handle. Therefore, the handle leakage is more harmful than the memory leakage.
The following describes how to use the Process class.
1. Use of the process class
Start starts a process and associates it with the process class.
Kill immediately shut down the process
WaitforExit: waiting for the exit of the associated process
Close to release all processes associated with this
Using System;
Using System. Collections. Generic;
Using System. Drawing;
Using System. Windows. Forms;
// Process class namespace
Using System. Diagnostics;
Namespace process
{
/// <Summary>
/// Description of MainForm.
/// </Summary>
Public partial class MainForm
{
[STAThread]
Public static void Main (string [] args)
{
Application. EnableVisualStyles ();
Application. SetCompatibleTextRenderingDefault (false );
Application. Run (new MainForm ());
}
Public MainForm ()
{
//
// The InitializeComponent () call is required for Windows Forms designer support.
//
InitializeComponent ();
//
// TODO: Add constructor code after the InitializeComponent () call.
//
}
// Start the IE homepage http://www.baidu.com/
Void Button1Click (object sender, System. EventArgs e)
{
Process. Start ("IExplore.exe", "http://www.baidu.com /");
}
// Start the Resource Manager
Void Button2Click (object sender, System. EventArgs e)
{
Process. Start ("assumer.exe ");
}
// Start EXCEl in the office
Void Button3Click (object sender, System. EventArgs e)
{
Process. Start ("EXCEL.exe ");
}
// Start the WINDOWS player
Void Button4Click (object sender, System. EventArgs e)
{
Process. Start ("dvdplay.exe ");
}
}
}
Using C # to achieve the same effect, we found that the convenient process thread mechanism of c # made the work extremely simple and easily recorded.
2. First, we can get the output interface by setting the Process class. The Code is as follows:
Process proc = new Process ();
Proc. StartInfo. FileName = strScript;
Proc. StartInfo. WorkingDirectory = strDirectory;
Proc. StartInfo. CreateNoWindow = true;
Proc. StartInfo. UseShellExecute = false;
Proc. StartInfo. RedirectStandardOutput = true;
Proc. Start ();
Then set the thread to continuously read the output string:
EventOutput = new AutoResetEvent (false );
AutoResetEvent [] events = new AutoResetEvent [1];
Events [0] = m_eventOutput;
M_threadOutput = new Thread (new ThreadStart (DisplayOutput ));
M_threadOutput.Start ();
WaitHandle. WaitAll (events );
The thread functions are as follows:
Private void DisplayOutput ()
{
While (m_procScript! = Null &&! M_procScript.HasExited)
{
String strLine = null;
While (strLine = m_procScript.StandardOutput.ReadLine ())! = Null)
{
M_txtOutput.AppendText (strLine + "\ r \ n ");
M_txtOutput.SelectionStart = m_txtOutput.Text.Length;
M_txtOutput.ScrollToCaret ();
}
Thread. Sleep (100 );
}
M_eventOutput.Set ();
}
Note that the following statements are used to make TextBox display always the latest, while AppendText does not use + =, it is because + = causes the display of the entire TextBox to flash the entire display area.
M_txtOutput.AppendText (strLine + "\ r \ n ");
M_txtOutput.SelectionStart = m_txtOutput.Text.Length;
M_txtOutput.ScrollToCaret ();
In order not to block the main thread, you can put the whole process in one thread or another.
3. Methods for bat file control parameters:
Write your net use http://www.cnblogs.com/acis_/admin/file://172.16.17.1//user: username password to the bat file and run the following code.
System. Diagnostics. Process process = new System. Diagnostics. Process ();
Process. StartInfo. CreateNoWindow = false;
Process. StartInfo. FileName = "d :\\ netuse. bat ";
Process. Start ();
Program Control Parameter Method:
System. Diagnostics. ProcessStartInfo psi =
New System. Diagnostics. ProcessStartInfo ();
// Prompt
Psi. FileName = @ "C: \ WINDOWS \ system32 \ cmd.exe"; // Path for the cmd prompt
Psi. Arguments mailto: =@% 22net use http://www.cnblogs.com/acis_/admin/file://172.16.17.1//user: username password ";
Psi. WindowStyle = System. Diagnostics. ProcessWindowStyle. Hidden;
System. Diagnostics. Process. Start (psi );
Use the process to start cmd.exe
An issue about using the Process class to run ShellExecute (click to view references)
Only ShellExecute on the stathread can ensure that the job is correct. This problem is not taken into account in the implementation of Process, so using the Process class to run ShellExecute may cause errors. If you cannot guarantee the ApartmentState of the thread that calls Process. Start, you can use the following code to avoid this problem:
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 ();
}
}