C#_ Threads and processes

Source: Internet
Author: User
Tags instance method thread class cpu usage

Process

A process is a dynamic execution of an application.

Namespaces--Diagnostics

Class--   Process

            Process.Start ("Calc");//Open Calculator

  Disadvantages : Some procedures of the registration name do not know, filled the wrong program error!

Code:

Instantiating a Process class
    Process P = new process ();
Create a process open Path object
    ProcessStartInfo psi = 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 ();

Overall the process is not very useful.

Thread

By using the System.Threading.Thread class, you can start a new thread and run a static or instance method on the threads stack.

  Sometimes the computer appears suspended animation because the main thread is occupied.

  Create the thread class and use the delegate to specify which function the thread is going to perform
  Thread th = new thread (delegate);
Turn on thread execution and not execute immediately
  Th. Start ();

  

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

Turn off the surveillance, how do you close it? In the constructor, the instantiation method is followed by this sentence: 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 the background thread th. IsBackground = true;

A. Starting a thread
As the name implies, "Startup thread" is the meaning of creating and starting a thread, which is implemented as follows:
thread thread1 = new Thread (new ThreadStart (Count));
where Count is the function that will be executed by the new thread.
B. Kill a thread
"Kill a Thread" is to extermination a thread, in order not to waste effort, it is best to determine whether it is still alive (via the IsAlive property) before killing it, and then call the Abort method to kill the thread.
c. Suspending a thread
It means letting a running thread sleep for a while. such as thread. Sleep (1000); is to have the thread hibernate for 1 seconds.
D. Priority level
There 's no need to explain this. HreadPR in thread classthe Iority property, which is used to set the priority, but does not guarantee that the operating system will accept the priority level. The priority of a thread can be divided into 5 types: Normal, AboveNormal, BelowNormal, highest, Lowest. Examples of specific implementations are as follows:
thread. priority = Threadpriority.highest;
E. Suspending a thread
the Suspend method of the thread class is used to suspend the thread until the resume is called and the thread can continue execution. If the thread is already suspended, it will not work.
if (thread. ThreadState = threadstate.running)
{
thread. Suspend ();
}
F. Recovering Threads
used to recover a suspended thread so that it continues to execute and will not work if the thread is not suspended.
if (thread. ThreadState = threadstate.suspended)
{
Thread. Resume ();
}

C#_ Threads and processes

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.