The process class is located in the system. Diagnostics namespace and is used to manage system processes. You can start and stop a process on a local computer, or query information of a specific type from the process. You cannot start or stop a process on a remote computer, but you can query the process information. When operating a process, you must first create an instance of the Process class, set the startinfo attribute of its object member, and call its start method.
For example, start, stop, and observe the process
1. Create a Windows application named processexample.
2. Drag and Drop the process component from the toolbox to the design form.
3. Add a namespace:
Using system. diagnostics;
Using system. Threading;
4. Add the "Start Notepad", "Stop Notepad", and "Observe all processes" buttons, and add the Click Event code:
Private void buttonstart_click (Object sender, eventargs E)
{
Process1.startinfo. filename = "notepad.exe ";
// Start the notepad.exe process.
Process1.start ();
}
Private void buttonstop_click (Object sender, eventargs E)
{
// Create an array of new process components and associate them with all process resources of the specified process name (Notepad.
Process [] myprocesses;
Myprocesses = process. getprocessesbyname ("Notepad ");
Foreach (process instance in myprocesses)
{
// Set to wait 1000 milliseconds before terminating the current thread
Instance. waitforexit (1000 );
Instance. closemainwindow ();
}
}
Private void buttonview_click (Object sender, eventargs E)
{
Listbox1.items. Clear ();
// Create an array of the Process type and associate them with all processes in the system
Process [] processes;
Processes = process. getprocesses ();
Foreach (PROCESS p in processes)
{
// Exclude idle because the starttime of the access to idle is abnormal.
If (P. processname! = "Idle ")
{
// Add each process name and process start time to listbox1
This. listbox1.items. Add (
String. Format ("{0,-30} {1: H: M: s}", p. processname, P. starttime ));
}
}
}
In the system. Threading namespace, the thread class is used to create and control threads. Common Operations on threads include starting threads, terminating threads, merging threads, and sleep threads.
Example: Use a thread in a program
1. Create a Windows application named threadexample.
2. Drag and Drop a timer component to the design form.
3. namespace: using system. Threading;
4. Field declaration:
Stringbuilder sb = new stringbuilder ();
Thread thread1;
Thread thread2;
5. Code:
Private void appendstring (string S)
{
Lock (SB)
{
Str. append (s );
}
}
Public void Method1 ()
{
While (true)
{
Thread. Sleep (100); // The thread sleep for 100 milliseconds
Appendstring ("");
}
}
Public void method2 ()
{
While (true)
{
Thread. Sleep (100); // The thread sleep for 100 milliseconds
Appendstring ("B ");
}
}
6. Add code to the Click Event of the start thread and terminate thread button:
Private void buttonstart_click (Object sender, eventargs E)
{
SB. Remove (0, SB. Length );
Timer1.enabled = true;
Thread1 = new thread (New threadstart (Method1 ));
Thread2 = new thread (New threadstart (method2 ));
Thread1.start ();
Thread2.start ();
}
Private void buttonabort_click (Object sender, eventargs E)
{
Thread1.abort ();
Thread1.join (10 );
Thread2.abort ();
Thread2.join (10 );
}
7. Tick Event code of timer1:
Private void timereffectick (Object sender, eventargs E)
{
If (thread1.isalive = true | thread2.isalive = true)
{
Richtextbox1.text = sb. tostring ();
}
Else
{
Timer1.enabled = false;
}
}
Compile and execute, click Start thread, and then click terminate.
Thread to view the running result.
By default, C # does not allow direct operations on controls in the other thread to prevent deadlock and other unsafe factors. However, in Windows applications, to display the information processed in a thread on a form, we may need to reference the Form Control in another thread frequently. A common method is to use a delegate to complete this job.
How can this be achieved?
Appendix:HandleTo Switch Applications
Process [] pS;
Process PS1 = new process ();
PS = process. getprocesses ();
For (INT I = 0; I <= ps. Length-1; I ++)
{
MessageBox. Show ("" + PS [I]. processname );
If (PS [I]. processname = "QQ ")
{
MessageBox. Show ("" + PS [I].Handle);
Return;
}
}
This code was found online and not verified.