简体   繁体   中英

Process,Kill() Not Working

I'm trying to make a service, that every 1000milliseconds(1 Second) Kills any process with the name Skype. I'm 100% sure I have coded it all correctly, everything is working, logging etc. But killing the process is not.

    private System.Timers.Timer _timer;
    protected override void OnStart(string[] args)
    {
        try
        {
            _timer = new System.Timers.Timer(100);
            _timer.Elapsed += _timer_Elapsed;
            _timer.Enabled = true;
            if (!EventLog.SourceExists("MYTESTSERVICE"))
                EventLog.CreateEventSource("MYTESTSERVICE", "MYTESTSERVICE LOG");
            //_timer.Start();
            Process[] p = Process.GetProcessesByName("Skype");
            foreach (Process proc in p)
            {

                proc.Kill();
                proc.WaitForExit();
                EventLog.WriteEntry("User Tried To Start Skype! Closed.", EventLogEntryType.Warning);

            }
        }
        catch (Exception ex)
        {
            EventLog.WriteEntry(String.Format("WcfServiceHostTest \n Exception Message: {0}\nTrace: {1}", ex.Message, ex.StackTrace), EventLogEntryType.Error);
        }
    }

I am currently not using the timer, but instead testing it upon service startup. If Skype is running, the process is not killed. I even tried notepad, and it did not kill that either. I have done research on this, but have not found an answer.

Any help is appreciated!

- Seb

you can use the taskkill:

Process.Start(new ProcessStartInfo
    {
        FileName = "taskkill",
        Arguments = $"/im skype.exe /f /t",
        CreateNoWindow = true,
        UseShellExecute = false
    }).WaitForExit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM