Delay 함수 대신 Thread.Sleep(2000) 해도 됨

메인 스레드 멈추는게 싫어서 인터넷에서 검색해서 Delay 함수 써봤는데

나의 고물 노트북은 CPU가 화가 많이 나더라..(특히 무한루프 환경에서)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Text;
using System.Net.NetworkInformation;
using System.IO;
using System.Net;
using System.Net.Sockets;
namespace PingTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //HJH 
            string srcIP = "";
            string dstIP = System.Configuration.ConfigurationManager.AppSettings["IP"];
            int iCount = 0;
 
            IPHostEntry host;
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                {
                    srcIP = ip.ToString();
                }
            }
 
 
            Ping pingSender = new Ping();
            PingReply reply = pingSender.Send(dstIP);
            while (true)
            {
                //app.config에 경로 지정 해야함. ex) "E:\pingtest\" 경우 E 드라이브 아래에 pingtest 폴더가 존재 해야함
                string sPath = System.Configuration.ConfigurationManager.AppSettings["FilePath"];
                string sTime = string.Format("{0:yyyyMMdd HHmm}", DateTime.Now);
 
                sPath += sTime + ".txt";
                File.AppendAllText(sPath, "********************************************************\r\n"Encoding.Default);
                File.AppendAllText(sPath, "SRC IP : " + srcIP + @"  /  DST IP : " + dstIP + "\r\n"Encoding.Default);
                File.AppendAllText(sPath, "********************************************************\r\n"Encoding.Default);
                Console.WriteLine("PING TEST - seq :" + Convert.ToString(iCount));
                while (true)
                {
                    Delay(2000);
                    if (reply.Status == IPStatus.Success) //핑이 제대로 들어가고 있을 경우
                    {
                        Console.WriteLine("Address: {0}", reply.Address.ToString());
                        Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
                        Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
                        Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
                        Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
 
                        File.AppendAllText(sPath, Convert.ToString(DateTime.Now) + "\r\n"Encoding.Default);
                        File.AppendAllText(sPath, "************RoundtripTime : " + reply.RoundtripTime.ToString() + "ms\r\n\r\n"Encoding.Default);
 
                    }
                    else //핑이 제대로 들어가지 않고 있을 경우 
                    {
                        Console.WriteLine(reply.Status);
 
                        File.AppendAllText(sPath, Convert.ToString(DateTime.Now) + "\r\n"Encoding.Default);
                        File.AppendAllText(sPath, "************ERROR : " + reply.Status.ToString() + "\r\n\r\n"Encoding.Default);
                    }
                    if (iCount % 10 == 0)
                    {
                        if (iCount != 0)
                            Console.WriteLine("PING TEST - seq :" + Convert.ToString(iCount));
                    }
                    if (iCount == 450)
                    {
                        iCount = 0;
                        break;
                    }
                    else
                    {
                        iCount++;
                    }
                }
            }
        }
        private static DateTime Delay(int MS)
        {
            DateTime ThisMoment = DateTime.Now;
            TimeSpan duration = new TimeSpan(0000, MS);
            DateTime AfterWards = ThisMoment.Add(duration);
 
            while (AfterWards >= ThisMoment)
            {
                //System.Windows.Forms.Application.DoEvents();
                ThisMoment = DateTime.Now;
            }
            return DateTime.Now;
        }
    }
}
 
cs

 

app.config

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <appSettings>
      <add key="IP" value="168.126.63.1" />
      <add key="FilePath" value="E:\pingtest\"/>
    </appSettings>
</configuration>