C # application process startup and shutdown

Source: Internet
Author: User

Sometimes there is a need, a process that only starts one, and must start one.

You can create a Windows service that executes at timed intervals. The number of application processes is queried each time it executes, if not, one is started, and if more than one is started, only one is left

Nonsense not much to say, on the code:

1   Public classClass12     {3         Public voidMain ()4        {5            varObj=system.diagnostics.process.getprocessesbyname ("Calc"). ToList ();6            if(obj. Count = =0)7 startprocess ();8            Else if(obj. Count = =1)9Systemlog.writelogline ("only one process starts, no action required");Ten            Else One killprocess (); A startprocess (); -        } -  the         Public voidstartprocess () -        { -ProcessStartInfo info =NewProcessStartInfo (); -Info. FileName =@"D:\Calc.exe"; +Info. Arguments =""; -Info. WindowStyle =processwindowstyle.minimized; +Process Pro =Process.Start (info); A            //Pro.  WaitForExit ();  at        } -         Public voidkillprocess () -        { -Process current =process.getcurrentprocess (); -Process[] processes = Process.getprocessesbyname ("Calc"); -            foreach(Process processinchprocesses) in            { -                if(Process. ProcessName = ="Calc") to process. Kill (); +            }    -        } the}

C # application process startup and shutdown

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.