OK. Now I will post the code for the timer function of the shutdown Applet and share my experiences with you.
First of all, it takes only three hours to write and complete the writing and debugging. Why did it take four days to renew? The main reason is that the information obtained in the previous three days has completely found the wrong direction. We will talk about the content later and read the code first.
- #include <windows.h>
- #include <ctime>
- #include <iostream>
- using namespace std;
-
- int Time_Tick()
- {
- SYSTEMTIME sys_time;
- GetLocalTime(&sys_time);
- cout<<"now the time is:"<<sys_time.wHour<<sys_time.wMinute<<sys_time.wSecond<<"\n";
-
-
- time_t time_now,time_all,time_user_H,time_user_M,time_user_S;
-
- cout<<"your time...\n";
- cout<<"Hour:";
- cin>>time_user_H;
- cout<<"Minute:";
- cin>>time_user_M;
- cout<<"Second:";
- cin>>time_user_S;
- time_now=time(NULL);
- time_all=time_user_H*3600+time_user_M*60+time_user_S+time_now;
-
- LOOP:
- time_now=time(NULL);
- if(time_now == time_all)
- {
- return 1;
- }else
- {
- goto LOOP;
- }
- }
The above is all the code. Now we can analyze it in small pieces.
First of all, we noticed that the time-related functions I used are as follows:
First
- SYSTEMTIME sys_time;
- GetLocalTime(&sys_time);
Second
- time_t time_now,time_all,time_user_H,time_user_M,time_user_S;
-
- time_now=time(NULL);
For analysis, the first code uses an api to obtain the current time of the system. In the second code, time () is used and the current system time is also obtained, the returned result is of the time_t type and is a large integer, but its value indicates the number of seconds from the Epoch Time of the UNIX system at 00:00:00, January 1, January 1, 1970) to the current Time. Therefore, you can write a small program to perform an experiment. The time () function outputs a large number. Note that you need to add the # include <time. h> used in C language programming) or # include <ctime> used in C ++ programming ). For more details, referLatest MSDN version
Let's take a look at how to implement timing. Pay attention to the following code:
- time_t time_now,time_all,time_user_H,time_user_M,time_user_S;
-
- cout<<"your time...\n";
- cout<<"Hour:";
- cin>>time_user_H;
- cout<<"Minute:";
- cin>>time_user_M;
- cout<<"Second:";
- cin>>time_user_S;
- time_now=time(NULL);
- time_all=time_user_H*3600+time_user_M*60+time_user_S+time_now;
-
- LOOP:
- time_now=time(NULL);
- if(time_now == time_all)
- {
- return 1;
- }else
- {
- goto LOOP;
- }
This part is the implementation of the timing function. Let's analyze it: Use time_user_H, time_user_M, time_user_S to get the time you need to set. Then, time_now is used to get the current time of the system, time_all is used to calculate the system time when an event is triggered, and converts the hour, minute, and second values in the preset time to seconds accordingly. In the LOOP, the system time is continuously obtained and compared with time_all. When the two are equal, the event is triggered. OK, the program logic is like this. Naturally, efficiency is quite low. If a friend has a better idea and a more efficient way of writing, I would like to share with you that I am also a fan of extracting system resources, thank you first ~.
Well, the point of attention is over. It's so simple, it's really so simple. So the question I asked: why have I spent more than four days? The answer is: Find the wrong direction. At first, I only wanted to look for ready-made functions. However, after consulting from multiple parties, I got the answer: there is no ready-made function for timed triggering of time, which needs to be implemented by myself. So this is the transfer direction. I hope that you will learn from me and will not change your perspective if you are looking for something fruitless for a long time. In fact, things are also very simple.
Of course, if a friend knows that a ready-made function can implement the function of triggering events on a regular basis, I would like to give you some advice. The next step is to implement the network traffic monitoring function module. In my urine, it won't take half a month. Let's wait.
This article is from the "cat nest" blog, please be sure to keep this source http://moowoo.blog.51cto.com/2665885/537783