윈도우 예약 종료 프로그램 제작 강좌 (C#)



using System;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace ReservedShutdown
{
    class Program
    {
        static void Main(string[] args)
        {
            string timeString;

            if (args.Length >= 1)
            {
                timeString = args[0];
            }
            else
            {
                Console.Write("종료 시각을 입력하세요(##:##:##): ");
                timeString = Console.ReadLine().Trim();
            }

            Regex r = new Regex("^(\\d{2}:){2}\\d{2}$");

            if (r.IsMatch(timeString))
            {
                r = new Regex("(\\d{2}):(\\d{2}):(\\d{2})");
                var match = r.Match(timeString);
                int h, m, s;

                h = int.Parse(match.Groups[1].Value);
                m = int.Parse(match.Groups[2].Value);
                s = int.Parse(match.Groups[3].Value);

                if (h < 0 || m < 0 || s < 0)
                {
                    Console.WriteLine("잘못 입력하셨습니다.");
                    return;
                }

                if (s >= 60)
                {
                    int tmp = s / 60;
                    m += tmp;
                    s -= 60;
                }

                if (m >= 60)
                {
                    int tmp = m / 60;
                    h += tmp;
                    m -= 60;
                }

                int seconds = h * 3600 + m * 60 + s;

                BackgroundWorker worker = new BackgroundWorker();
                worker.DoWork += Tick;

                worker.RunWorkerAsync(seconds);

                Console.ReadLine();
            }
            else
            {
                Console.WriteLine("잘못 입력하셨습니다.");
                System.Threading.Thread.Sleep(5000);
            }
        }

        static long lastTick = 0;
        private static void Tick(object sender, DoWorkEventArgs e)
        {
            int seconds = (int)e.Argument;

            long now = DateTime.Now.Ticks / TimeSpan.TicksPerSecond;

            long dest = now + seconds;


            while ((now = DateTime.Now.Ticks / TimeSpan.TicksPerSecond) <= dest)
            {
                if (lastTick + 1 <= now)
                {
                    Console.Clear();
                    int h, m, s;

                    int remain = (int)(dest - now);

                    h = remain / 3600;

                    m = remain / 60 % 60;

                    s = remain % 60;

                    Console.WriteLine($"종료까지 {h:00}시간 {m:00}분 {s:00}초 남았습니다.");

                    lastTick = now;
                }
            }

            Console.Clear();

            var process = new System.Diagnostics.Process();
            process.StartInfo.UseShellExecute = true;
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = "/c shutdown -s -t 0";

            process.Start();
        }
    }
}

댓글

이 블로그의 인기 게시물

C# 남아도는 메모리에도 불구하고 OutOfMemoryException이 발생한다면?

MySQL 데이터 타입과 Java 데이터 타입 비교/매칭

테일즈위버 OST 전곡 모음