About the pits of functions such as AddSeconds that come with DateTime

Here is a record of the DateTime problems encountered when writing a process killing applet today:

 The first time is to use the time after AddSeconds to directly judge the equality with DateTime.Now.

 1    class Program
 2     {
 3         static void Main(string[] args)
 4         {
 5             Program pg = new Program();
 6             pg.NextRunTime = DateTime.Now.AddSeconds(2);
 7             while (true)
 8             {
 9                 if (DateTime.Now == pg.NextRunTime)
10                 {
11                     Thread.Sleep(500);
12                     pg.KillPro();
13                     pg.NextRunTime = DateTime.Now.AddSeconds(5);
14                 }
15             }
16         }
17 
18         private void KillPro()
19         {
20             int count = 0;
21             foreach (Process p in Process.GetProcesses())
22             {
23                 count++;
24                 //if (p.ProcessName == "werfault.exe")
25                 //{
26                 //    p.Kill();
27                 //}
28                 if (p.ProcessName == "RemotingServerConsoleContainer")
29                 {
30                     Console.WriteLine(p.ProcessName+"get killed");
31                     p.Kill();
32                     count = 0;
33                 }
34             }
35             if (count != 0)
36             {
37                 Console.WriteLine("RemotingServerConsoleContainer is not running");
38             }
39         }
40 
41         private DateTime nextRunTime;
42 
43         public DateTime NextRunTime
44         {
45             get;
46             set;
47         }
48     }
Time format direct equality judgment

I want to judge whether to execute the program by comparing the current execution time and the next execution time. The result can't get into the if judgment.

Later , I found that the equality judgment of DateTime cannot use ==, and should use DateTime.

 1  public static void Main()
 2         { 
 3             DateTime t1 = DateTime.Now.AddSeconds(3.0);
 4             while (true)
 5             {
 6                 if (t1.CompareTo(DateTime.Now) == 0)
 7                 {
 8                     Thread.Sleep(500);
 9                     Console.WriteLine("111");
10                     t1 = DateTime.Now.AddSeconds(3.0);
11                 }
12                 if (t1.ToString() == DateTime.Now.ToString())
13                 {
14                     Thread.Sleep(500);
15                     Console.WriteLine("222");
16                     t1 = DateTime.Now.AddSeconds(3.0);
17                 }
18             }
19         }
The second time I take it for granted CompareTo

As a result, the program runs and only prints "222";

That is, the first judgment is not equal (nonsense), and then look carefully at the AddSeconds() function, it is of the double type added, thinking of the double type pit, the 3 you added may not be the real 3, use the following two line of code to check

DateTime t1 = DateTime.Now.AddSeconds(3.0f);
Console.WriteLine((t1 - DateTime.Now));

The result is needless to say, as shown in the figure

Summarize:

1. When using the method, pay attention to the method parameters. If you happen to encounter this parameter type, which is a floating-point type, congratulations, you should pay attention to the fact that floating-point type is not what you see is what you get, but it gives you the rounded result. .

2. If the date format is judged to be equal, it can be converted to ToString() to judge; of course, there are other better methods, and the specific situation is analyzed in detail.

3. I have not really understood the use of attributes, and reflect on my technical foundation. Honestly watch the article, multi-bridge code, and think more.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325305455&siteId=291194637