In-depth analysis of WinForm processes, threads and differences, analysis of winform

Source: Internet
Author: User

In-depth analysis of WinForm processes, threads and differences, analysis of winform

I. 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"); // open the calculator Process. start ("mspaint"); // open the drawing Process. start ("iexplore", "http://www.baidu.com"); // open the browser and specify the address

(1) 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 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) Use the file selection box to allow users to select the desired program and open it:

Private void button#click (object sender, EventArgs e) {// This is the selected file type openFileDialog1.Filter = "application | *. exe "; // display the dialog box and determine whether the user has selected the file if (openFileDialog1.ShowDialog () = DialogResult. OK) {// obtain the file path string path = openFileDialog1.FileName; // create a new Process, Process p = new Process (); // ProcessStartInfo psf = new ProcessStartInfo (path); // sets the execution information of a manufacturing process. startInfo = psf; // start the process p. start ();}}

Example: Logout

// This path is the absolute path of the exe program to be called. string path = Application. startupPath; // get the file name path of your own exe or dll string s = System. reflection. assembly. getExecutingAssembly (). manifestModule. fullyQualifiedName; private void button2_Click (object sender, EventArgs e) {// open the program // obtain the path of the program file // string path = Application. startupPath; string path = System. reflection. assembly. getExecutingAssembly (). manifestModule. fullyQualifiedName; // create a Process p = new Process (); // create a Process startup information ProcessStartInfo ps = new ProcessStartInfo (path); // set Process startup information p. startInfo = ps; // start the process p. start (); // close the program this. close ();}

For example, dragging the mouse over a window

Private int x; private int y; private void picturebox#mousedown (object sender, MouseEventArgs e) {// XY coordinate x = e when the mouse is clicked. x; y = e. y;} private void picturebox#mousemove (object sender, MouseEventArgs e) {// determines if (e. button = System. windows. forms. mouseButtons. left) {// margin = current distance + moving distance this. left = this. left + (e. x-x); this. top = this. top + (e. y-y );}}

Ii. 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:

Which function does the new thread start to execute?

Thread th = new Thread (the method in which the Thread is written for operation. The method name does not contain parentheses );

Mark that this 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 thread-the program can be closed only when all foreground threads are closed.

Background thread-as long as all foreground threads end, background threads end automatically.

The difference between the two: the application can exit only after running all foreground threads;

For background threads, the application can exit directly without considering whether it has been completed. All background threads will automatically exit when the application exits.

End.

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 restriction

In the interface Load event

Control. checkforillegalcrossthreadcils = false;

In the FormClosing event:

// Determine whether the new thread is null. if it is null, 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 the method executed by the thread, the parameter must be of the object type!

Thread th = new Thread (Test); // The parameter name does not need to be enclosed in parentheses. 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 ;    ......  }

The above is a brief introduction to WinForm processes, threads, and differences. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.