A simple timer is made with the thread of C. In order to allow yourself to take a rest in 45 minutes, a music prompt will be triggered after 45 minutes.
The timespan Subtraction Method Used at the beginning is also written in the thread startup function as follows:
- Public VoidCounttime ()
- {
- While(True)
- {
- Timespan tsnew =NewTimespan (datetime. Now. ticks );
- Timespan Tsin = tsnew-tsold;
- If(Tsin. Minutes> = 1)
- {
- While(True)
- {
- Timespan tsnewer =NewTimespan (datetime. Now. ticks );
- Timespan tsiner = tsnewer-tsnew;
- If(Tsiner. Minutes> = 10)
- {
- // The thread restarts in 10 minutes
- Tsold = tsnew;
- Break;
- }
- }
- }
- }
- }
Later, we found that this method is too inefficient. Of course, you can use thread. Sleep (20); this function reduces the CPU usage time. In fact, adding thread. Sleep (20) in the middle can significantly reduce CPU consumption. Later, we found that thread in C # had a self-contained function join (), which could make the thread wait for a while. The call 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 also call thread. Sleep (millionseconds). The difference between sleep and join is described here.
When a thread executes sleep, the system exits the execution queue for a period of time. When sleep ends, the system generates a clock interruption, which causes the thread to return to the execution queue to resume the thread execution.If the sleep method is set to 0, it indicates that the thread should be suspended for other waiting threads to run. Here, the CPU re-allocates control, or it may be the program just executed. In this way, the CPU usage is always 100%.Sometimes your interface is dead, but you can still move the mouse. If it is timeout. Infinite, it means that the thread will sleep until it is interrupted by another thread called thread. interrupt or it is aborted by thread. Abort.
If the parent thread ends before the child thread, the Child thread is forced to end when the parent thread ends. The thread. Join () method waits for the parent thread until the child thread ends. The join method has a return value. The value is true, indicating that the thread is terminated. False indicates that the thread has not been terminated after waiting for that time. If you call join () when the thread is in the unstarted state, there will be a threadstateexception. If the thread has been terminated, call this function and get the return value immediately.
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 program is that it is possible that the thread has not ended after the main program exits. (This slave thread is really poor ...)
Here, I will mention several states of the thread:
Create: when a new process is created, a thread is also created for the process. The thread can also create a new thread.
Ready: the thread has obtained all resources except the processor.
Running: The thread is being executed on the processing machine.
Blocking: the thread stops running because it is waiting for an event.
Termination: A thread has been completed.
However, the C # thread has several more states:
Aborted, abortrequested, background, running, stopped, stoprequested, suincluded, suspendrequested, unstarted, waitsleepjoin.
Abort () will cause threadstate. abortrequested to call the abort () thread to obtain control, resulting in threadstate. Aborted. The difference between abortrequested and aborted is that one stop is not stopped. Stopped indicates that the thread is terminated. However, I tried multiple times and found that when the abort () function is called, the thread status changes to stopped. How to change to the aborted state is still under study. Other statuses are similar. All of them call the corresponding function to generate the corresponding status request. It will take some time before the corresponding status can be reached. As for unstarted, the START () function has not been called, and running is the result of calling the START () or resume () function. Waitsleepjoin is waiting for I/O or calling the join () method.Here, the difference between the join () and sleep () methods lies in the call of the join () thread status to waitsleepjoin, and the call of the sleep () thread status to running.
The thread suspension method is suspend (). After this method is called, the thread is in the suspendrequest state. After suincluded () is called, if the thread is still executing the join () method, it can suspend the thread only after the thread reaches the security point, in this case, the thread state is suspendrequested | waitsleepjoin. However, the clock in join is still counted. Therefore, we do not know how to pause the join count. Of course, we can also choose not to use the join statement to completely pause the thread. I don't know which suincluded () function is for now, because the thread is still running. It is also worth mentioning that currently we do not advocate the use of suspend () and the resume () method that is re-restored after the thread calls suspend.The reason is very simple, because these two methods are executed by another thread, and the other thread does not know exactly what state the suspend () thread is in, such as the execution period of the constructor of a class, or the structure. So it is dangerous to use this function for synchronization.
In addition, it should be noted that the thread. Sleep (n) n cannot accurately control the time. If you think the thread takes a long time, this control is problematic. If the current thread is a front-end thread, thread. Sleep (n) must be later than N to exit. If it is a background process, after the main program exits, this thread will no longer be able to wake up... miserable... so we generally recommend that you do not use the thread. Sleep () function. In addition, the sleep function cannot be used for synchronization. Peter said that the program's sleep function represents a bad design.
A timer has caused these problems... crazy...