In depth analysis of WinForm process thread and difference introduction
- 2021-08-16 23:38:32
- OfStack
1. Process
A process is a running activity of a program with independent functions about a data set.
It can apply for and own system resources, is a dynamic concept, is an active entity.
The Process class, used to manipulate processes.
Namespace: using System. Diagnostics;
Process.Start("calc"); // Open Calculator
Process.Start("mspaint"); // Open drawing
Process.Start("iexplore" , "http://www.baidu.com"); // Open a browser and specify an address
(1) Open the specified file through 1 process:
1. Create a process object
Process p = new Process();
2. Create an StartInfo object that specifies the path with a drive letter.
ProcessStartInfo psi = new ProcessStartInfo(@"C:\user\.....);
3. Process designation and start
p.StartInfo = psi; // Specify the path
p.Start(); // Start the process
(2) Let the user choose the program he needs to open and open it through the file selection box:
private void button1_Click(object sender, EventArgs e)
{
// This is the type of the selected file
openFileDialog1.Filter = " Applications |*.exe";
// Display a dialog box and determine whether the user has selected the file
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// Fetch the file path
string path = openFileDialog1.FileName;
// Create 1 A new process
Process p = new Process();
// Start-up information of manufacturing process
ProcessStartInfo psf = new ProcessStartInfo(path);
// Set the execution information of the process
p.StartInfo = psf;
// Start process
p.Start();
}
}
Example: Logout
// This path Is what you want to call exe Absolute path of program
string path = Application.StartupPath;
// Get oneself exe Or dll File name path of
string s = System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName;
private void button2_Click(object sender, EventArgs e)
{
// Open the program
// Take the path of the program file
//string path = Application.StartupPath;
string path = System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName;
// Make 1 Processes
Process p = new Process();
// Make 1 Startup information for each process
ProcessStartInfo ps = new ProcessStartInfo(path);
// Setting process startup information
p.StartInfo = ps;
// Start process
p.Start();
// Close program
this.Close();
}
Example: The window realizes mouse dragging
private int x;
private int y;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
// When the mouse clicks XY Coordinates
x = e.X;
y = e.Y;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
// Judge that the mouse presses the left key
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
// Margins = Current distance + Distance of movement
this.Left = this.Left+(e.X - x);
this.Top = this.Top +(e.Y - y);
}
}
2. Threads
Threads, sometimes referred to as lightweight processes (Lightweight, Process, LWP), are the smallest units of program execution flow.
Difference: Processes have their own independent resources, and threads share resources.
Thread class, namespace: using System. Threading;
The program has only one main thread by default. If you do complex processing, you will be suspended animation, because one thread can only do one thing at the same time.
Multi-thread function: Do many things at the same time, save time, run the program in the background, improve the running efficiency of the program, and will not make the main interface appear unresponsive.
To create a thread:
Open a new thread to execute which function
Thread th = new Thread( Write the method to be operated by the thread, and the method name does not contain parentheses );
Mark that this thread is ready to execute at any time, and 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 threads-As long as all foreground threads end, the background threads will automatically end.
The difference between the two: the application must run all the foreground threads before it can exit;
For background threads, the application can exit directly regardless of whether it has finished running, and all background threads will automatically when the application exits
End.
Set th thread as background thread
th . IsBackground = true ;
Control is created by the main thread, and the new thread wants to access the main thread resources. The program does not allow cross-thread access by default.
Cancel cross-thread access restrictions
In the interface Load event,
Control. CheckForIllegalCrossThreadCalls = false;
In the FormClosing event:
// Determines whether the new thread is null If it is null , then it automatically shuts down
if(th != null)
{
// End the thread immediately, but it cannot be restarted after it ends Start It's over
th.Abort();
}
Execute a function with arguments:
If a method executed by a thread requires a parameter, it must be of type object!
Process p = new Process();
0
This requires a strong turn in the function:
Process p = new Process();
1