我正在尝试创建一个服务,即每1000毫秒(1秒)杀死任何名为Skype的进程。我100%确定我的代码都是正确的,所有的东西都在工作,日志记录等等,但杀死进程却不是。
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);
}
}我目前没有使用计时器,而是在服务启动时测试它。如果Skype正在运行,则不会终止该进程。我甚至试过记事本,它也没有杀死它。我对此做了研究,但还没有找到答案。
如有任何帮助,我们不胜感激!
发布于 2017-05-19 03:20:21
您可以使用taskkill:
Process.Start(new ProcessStartInfo
{
FileName = "taskkill",
Arguments = $"/im skype.exe /f /t",
CreateNoWindow = true,
UseShellExecute = false
}).WaitForExit();https://stackoverflow.com/questions/36659587
复制相似问题