Detailed introduction of C#thread point drip

Source: Internet
Author: User
Tags sleep function
A simple timer was made with the thread of C #. In order to allow yourself to rest after 45 minutes, after 45 minutes will sound music tips.

The way to subtract from the timespan that you started using, in the thread's startup function, is this:

    1. public void Counttime () {               while (true)   {      TimeSpan tsnew = new TimeSpan (DateTime.Now.Ticks);      TimeSpan tsIn = tsnew-tsold;      if (tsin.minutes >= 1)      {             while (true)         {             TimeSpan tsnewer = new TimeSpan (DateTime.Now.Ticks);             TimeSpan tsiner = tsnewer-tsnew;             if (tsiner.minutes >=)             {                ///10 minutes after the thread restarts                 tsold = tsnew;                 Break;}}}}   

It was later found that the method was too inefficient. Of course, you can use Thread.Sleep (20); This function reduces CPU time. In fact, adding Thread.Sleep (20) in the middle, can significantly reduce CPU consumption. It was later discovered that the thread in C # had a function join (), which allowed the threads to wait for a while. The calling method is as follows

Th. Join (New TimeSpan (hours, minutes, seconds)); The thread is in the WaitSleepJoin state during the waiting period.

Of course you can call Thread.Sleep (millionseconds); Here's the difference between Sleep and join

When the thread executes sleep, the system exits the execution queue for a period of time, and when the sleep is over, the system generates a clock interrupt, which causes the thread to return to the execution queue for the execution of the recovery thread. the sleep method, if the parameter is 0, means that the thread should hang to allow other waiting threads to run, where the CPU is reassigned to control, or it may be the program that was just executed. This will happen when CPU usage is always 100%. sometimes your interface is dead, but you can still move the mouse. If it is timeout.infinite, it will cause the thread to hibernate until another thread that is called Thread.Interrupt is interrupted or aborted by Thread.Abort.
If the parent thread ends before the child thread, the child thread is forced to end at the end of the parent thread. The Thread.Join () method causes the parent thread to wait until the child thread ends. the join method has a return value of true, which represents the thread termination. False indicates that the thread has not terminated after waiting for that period of time. If the thread unstarted state, the call to join () will have a threadstateexception exception. If the thread has been terminated, the call to this function will immediately give the return value.

For example, the following main program

... ThreadStart st = New ThreadStart (fun); Thread th = new Thread (ThreadStart st); th. Start (); Application.exit (); ....//The following is the fun function void fun () {while    (true)    {            ...    }}

The only problem with this procedure is the possibility that the main program exits after the thread has not ended. (This from the line threads poor ...)

Here, incidentally, a few states of a thread:

Create: When a new process is created, a thread is also created for the process. Threads can also create new threads.

Ready: The thread has obtained all resources except the processor.

Run: The line is impersonating is executed on the processing machine.

Blocking: A thread pauses to run because it waits for an event.

Terminate: A thread has completed.

But there are several more states in C # threads:

Aborted,abortrequested,background,running,stopped,stoprequested,suspended,suspendrequested,unstarted, WaitSleepJoin.

Abort () will cause threadstate.abortrequested to call abort () After the thread gets control, the difference between threadstate.aborted,abortrequested and aborted is that a stop one is not stopped. And stopped is the thread termination. But I've tried it over and over again. Once the abort () function is called, the thread state becomes stopped. How to become a aborted state is still under study. Several other states are similar. is to call the corresponding function to generate the corresponding status request, there is a period of time to exactly the corresponding state. As for Unstarted, the start () function is not yet called, and running is the result of invoking the start () or Resume () function. WaitSleepJoin is waiting for I/O, or calling the join () method. The difference between the join () and sleep () methods here is also to call the join () thread state into WaitSleepJoin, call the sleep () thread state or running.

The method of suspending a thread is suspend (); After calling this method, the thread is in the Suspendrequest state. Suspended () After a call if the thread is still executing the join () method, because Suspended () will be able to suspend the thread until it reaches the security point, at which point the thread state is suspendrequested| WaitSleepJoin. But the clock in the join here is still counting. So it's not yet known how to pause the join count, and of course you can eliminate the problem of completely pausing the thread without using join. It's not clear what the suspended () function is doing, because the thread is still running. It is also worth mentioning that it is now not recommended to use Suspend () and resume () methods that let the thread call suspend () again. The reason is simple because the two methods are executed by another thread, and the other thread does not know exactly what state the thread of the suspend () is in, such as the time of the constructor execution of a class, or the destructor. So it's dangerous to use this function to synchronize.

In addition, it is important to note that Thread.Sleep (n) is not an accurate control of the N-time. This control is problematic if you think how long the thread will take. If the current thread is a foreground thread, then Thread.Sleep (n) will have to exit at a time greater than N. If it is a background process, the thread can no longer wake up when the main program exits. Tragic.. Therefore, it is generally recommended not to use the Thread.Sleep () function. In addition, the sleep function cannot be used for synchronization. Peter says the sleep function of the program represents a bad design.

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.