본문 바로가기

.NET/C#

c# Timer and Start Process Kill Process


간단하게 윈도우 프로그램을 


실행 종료시키는 Console 프로그램을 만들었다


아주 기초적인 거지만 혹시 필요하신 분은 


사용하면 괜찮을것같다.


using System;

using System.Collections.Generic;

using System.Diagnostics;

using System.Linq;

using System.Text;

using System.Threading;

using System.Threading.Tasks;


필요한 namespace를 등록한다.


 static void Main(string[] args)

        {

            var autoEvent = new AutoResetEvent(false);




            var stateTimer = new Timer((d) => {


                Process[] processList = Process.GetProcessesByName("WindowsFormsApp1.exe");

                if (processList.Length >0)

                {

                    processList[0].Kill();

                }

                Process.Start("C:\\Users\\igzemi\\Desktop\\Debug\\WindowsFormsApp1.exe");

            },

            autoEvent, 0, 600000);


        

            autoEvent.WaitOne();

          

            Console.ReadLine();

            stateTimer.Dispose();

        }


메인함수를 위와 같이 쓰자


간단히 설명하지만


Timer 객체를 만들고 거기에 인자값으로 


첫번째 delegate  두번째 resetEvent Argument 셋째 dueTime 바로실행? 넷째 시간인다 


위의 시간을 보면 10분 간격으로 WindowsFormsApp1.exe를 킬하고 다시 시작한다.


해당 케이스는 꼭 Process를 Kill해야해서 저렇게 쓴거다..


허접하지만 가져다 쓰실분은 쓰시라.