WinForm Processes and Threads

Source: Internet
Author: User

Process:

The class process used by processes needs to be parsed using System.Diagnostics

Process.Start ("Calc");//process is a non-static method with the name of the process to be opened in parentheses

(Static methods differ from non-static methods: Static methods do not instantiate objects, they can simply point out the method inside)

Cons: There are many programs do not know what its running name is, if written wrong, there will be a program crash error

A more useful way to open a process:

    

// instantiating a process class                    New Process ();                     // Create a process open Path object                    New ProcessStartInfo (TextBox1.Text);                     // Process Class Open path point                    P.startinfo = psi;                     // start the process, not immediately open, when it is determined                    by the CPU P.start ();

Why did the program pretend to be suspended? Because the main thread is occupied

Thread to execute a time-consuming code, the main thread will not be occupied, the program will not appear in the state of suspended animation

Methods for creating Threads:

// Create the thread class and use the delegate to specify which function the thread is going to perform New  // turn on thread execution function, nor do th immediately . Start ();

The two common problems encountered during the threading process and their workarounds are as follows:

Question one:
Threads can be turned on many, if the user can click the Unlimited button, then it may cause computer CPU usage is too high
With a thread execution method, only one parameter can be passed, and this parameter must be of type Object
program does not allow cross-thread access to objects by default
Shut down the surveillance, you can.


   How close?
In the constructor, add this sentence under the instantiation method:
    Control.checkforillegalcrossthreadcalls = false;

Question two:

The default main form is closed, but if the thread has not finished working, it silently resumes execution until the entire process is closed.

Program default all threads are: foreground thread

    Workaround: Turn the newly opened thread into a background thread
Th. IsBackground = true;

       

  Summarize:
The namespace in which the thread resides:system.threading;
Threading class: Thread th = new Thread (method name);
There is no parameterless constructor, commonly used for, 1 parameters, parameter type is a delegate type, requires a method to point to, become a background thread:
    Th. IsBackground = true;

Thread Start:
    Th. Start ();

If a method with parameters is to be executed, then the parameter of the method must be of type object, where the argument is passed in parentheses of the Start method

Allow access to objects across threads:
In the constructor constructor, add the following:
    Control.checkforillegalcrossthreadcalls = false;

To stop a thread:
    Th. Abort ();

WinForm Processes and Threads

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.