Shutdown applets-Implementation of the timing Function

Source: Internet
Author: User
Tags epoch time

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.

 

 
 
  1. #include <windows.h> 
  2. #include <ctime> 
  3. #include <iostream> 
  4. using namespace std; 
  5.  
  6. int Time_Tick() 
  7.     SYSTEMTIME sys_time; 
  8.     GetLocalTime(&sys_time); 
  9.     cout<<"now the time is:"<<sys_time.wHour<<sys_time.wMinute<<sys_time.wSecond<<"\n"; 
  10.  
  11.  
  12.     time_t time_now,time_all,time_user_H,time_user_M,time_user_S; 
  13.  
  14.     cout<<"your time...\n"; 
  15.     cout<<"Hour:"; 
  16.     cin>>time_user_H; 
  17.     cout<<"Minute:"; 
  18.     cin>>time_user_M; 
  19.     cout<<"Second:"; 
  20.     cin>>time_user_S; 
  21.     time_now=time(NULL); 
  22.     time_all=time_user_H*3600+time_user_M*60+time_user_S+time_now; 
  23.  
  24. LOOP: 
  25.     time_now=time(NULL); 
  26.     if(time_now == time_all) 
  27.     { 
  28.         return 1; 
  29.     }else 
  30.     { 
  31.         goto LOOP; 
  32.     } 

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

 
 
  1. SYSTEMTIME sys_time; 
  2. GetLocalTime(&sys_time); 

Second

 
 
  1. time_t time_now,time_all,time_user_H,time_user_M,time_user_S; 
  2.  
  3. 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:

 
 
  1. time_t time_now,time_all,time_user_H,time_user_M,time_user_S; 
  2.  
  3.     cout<<"your time...\n"; 
  4.     cout<<"Hour:"; 
  5.     cin>>time_user_H; 
  6.     cout<<"Minute:"; 
  7.     cin>>time_user_M; 
  8.     cout<<"Second:"; 
  9.     cin>>time_user_S; 
  10.     time_now=time(NULL); 
  11.     time_all=time_user_H*3600+time_user_M*60+time_user_S+time_now; 
  12.  
  13. LOOP: 
  14.     time_now=time(NULL); 
  15.     if(time_now == time_all) 
  16.     { 
  17.         return 1; 
  18.     }else 
  19.     { 
  20.         goto LOOP; 
  21.     } 

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

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.