The timer executes the code once at a certain time. Its execution depends entirely on the time. After the thread OS allocates memory space to it, it will completely wait in the thread queue like the main thread. Therefore, the timer is preemptive execution. As long as the time comes, it will execute immediately due to high priority, affecting the program execution speed, The thread will enter the queue with other threads.

Their uses are also different. Threads are used for concurrent execution and program synchronization, while timers only do some work that must be done regularly, such as refreshing windows. But if you use a timer to update the time, it will not pay off. Because it doesn’t matter to update 1 second or 2 seconds late (when the requirements are not strict).

So I want to open a thread to update the time.

1. Define your own macro for message mapping.

#define MSG_ MY_ TIME WM_ USER+1

2. Declare and define a thread function.

volaTIle bool TImeThreadRun

void TImeThreadFun(LPVOID lpParam)

{

TImeThreadRun = true;

while(TimeThreadRun)

{

PostMessage((HWND)lpParam, MSG_MY_TIME, 0, 0);

Sleep(1000);

}

}

3. Define message mapping and message mapping function in corresponding classes.

afx_ msg LRESULT OnMyTime(WPARAM, LPARAM);

BEGIN_ MESSAGE_ MAP(。。。)

ON_ MESSAGE(MSG_MY_TIME, OnMyTime)

END_ MESSAGE_ MAP()

LRESULT OnMyTime(WPARAM wp, LPARAM lp)

{

。。。。。。

}

4. Start a thread in a function. Where this – 《 m 》_ HWnd is the handle to this type of window.

hTimeThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)TimeThreadFun, (LPVOID)(this-》m_hWnd), 0, &timeThreadID);

5. You can get a multithreaded timer. When closing, set timethreadrun to false.

In fact. Net has a special timer for us, and the function can also meet most of our needs. However, sometimes we only need a simple timing requirement, but there are many parameters on the timer. What can we do if we can’t remember? At this time, we can use a simple thread to meet our needs.

First, let’s create a simple thread

Using thread to make simple timer

You can see this result after execution

Using thread to make simple timer

According to the requirements, if it only needs to be executed once, in fact, we just need to add a thread sleep before the thread execution method

Using thread to make simple timer

Then let’s see the results

Using thread to make simple timer

Sleep for three seconds and then execute the following methods. By this time, the thread has finished running.

If our demand is more than once, we can add some cycles appropriately

Using thread to make simple timer

Then you can see our execution after such a loop

Using thread to make simple timer

Look, can a simple scheduled task be executed like this

Some people may want to ask about a recycling problem after the thread is completed. First, c# is an object-oriented language. It comes with GC (garbage collector). The system will detect some variables within a certain time. If the object is useless for a long time, it will be cleared automatically.

Secondly, the thread will terminate automatically after running. The CLR will decide when to destroy it to release memory and other resources. Generally speaking, when your resources are insufficient or have higher priority requirements, the memory will be released to avoid affecting CPU performance. If you are really worried, use abort to stop it!

th.Abort();

Leave a Reply

Your email address will not be published. Required fields are marked *