Process and thread, process thread
Process:
A process is a running activity of a program with independent functions about a data set. It can apply for and possess system resources. It is a dynamic concept and an activity entity.
Process class, used to operate processes. Namespace: using System. Diagnostics;
Process. Start ("calc"); // Calculator
Process. Start ("mspaint"); // draw
Process. Start ("iexplore", "http://www.baidu.com"); // open the browser and specify the address
// Open a specified file through a process
// 1. Create a process object
Process p = new Process ();
// 2. Create a StartInfo object that specifies the path with the drive letter.
ProcessStartInfo psi = new ProcessStartInfo (@ "C: \ user \.....);
// 3. Specify and start the process
P. StartInfo = psi; // specify the path
P. Start (); // Start the process
In the file selection box, you can select the program to be opened and open it:
// Set the file type to open
OpenFileDialog1.Filter = "application | *. exe ";
// Determine whether to select a program file
If (openFileDialog1.ShowDialog () = DialogResult. OK)
{
// Obtain the file path
String path = openFileDialog1.FileName;
// Create a process
Process p = new Process ();
// Set the startup information
ProcessStartInfo ps = new ProcessStartInfo (path );
P. StartInfo = ps;
// Start the process
P. Start ();
}
Bytes -----------------------------------------------------------------------------------------------
Example: Logout
// String path = Application. StartupPath; // This path is the absolute path of the exe program to be called.
// Obtain the path of the file name of the self-built exe or dll
String s = System. Reflection. Assembly. GetExecutingAssembly (). ManifestModule. FullyQualifiedName;
// Create a process
Process p = new Process ();
// Set process startup information
ProcessStartInfo ps = new ProcessStartInfo (s );
P. StartInfo = ps;
// Start the process
P. Start ();
// Close the current program
This. Close ();
Bytes ----------------------------------------------------------------------------------------------
// Implement mouse dragging
Int x;
Int y;
Private void picturebox#mousedown (object sender, MouseEventArgs e)
{
X = e. X;
Y = e. Y;
}
Private void picturebox#mousemove (object sender, MouseEventArgs e)
{
If (e. Button = System. Windows. Forms. MouseButtons. Left)
{
This. Left = this. Left + (e. X-x );
This. Top = this. Top + (e. Y-y );
}
}
Bytes ----------------------------------------------------------------------------------------------
Thread:
A thread, sometimes called a Lightweight Process (LWP), is the smallest unit of the program execution flow.
Difference: processes have their own independent resources, and threads share resources.
Thread class, namespace: using System. Threading;
By default, the program has only one main thread. If complicated processing is performed, the program will be suspended because one thread can only do one thing at the same time.
The role of multithreading: To do multiple things at the same time, save time, run the program in the background, improve the running efficiency of the program, and do not cause no response on the main interface.
Creation thread:
// Enable the new thread to execute which function
Thread th = new Thread (the method in which the Thread is written for operation. The method name does not contain parentheses );
// Mark that the thread is ready and can be executed at any time. The CPU determines the execution time.
Th. Start ();
There are two types of threads: foreground threads-only when all foreground threads are closed can the program be closed.
Background thread-as long as all foreground threads end, background threads end automatically.
The difference between the two is that the application can exit only after running all the foreground threads. For background threads, the application can exit without considering whether it has been completed, all background threads automatically end when the application exits.
// Set th thread to background thread
Th. IsBackground = true;
The control is created by the main thread. When a new thread wants to access the resources of the main thread, the program does not allow cross-thread access by default.
// Cancel cross-thread access restrictions
// In the interface Load event
Control. checkforillegalcrossthreadcils = false;
In the FormClosing event:
// Determines whether the new thread is null. If it is null, it is automatically disabled.
If (th! = Null)
{
// End the thread immediately, but the thread cannot be restarted after the end
Th. Abort ();
}
Execute a function with parameters:
* **************** If a parameter is required for a method executed by a thread, the parameter must be of the object type! *******************
Thread th = new Thread (Test); // parameter names do not need to be enclosed by brackets
Th. IsBackground = true;
Th. Start ("123"); // input the parameters required by the Test function.
In this way, the function requires strong conversion:
Private void Test (object s)
{
String ss = (string) s;
......
}