[英]How can i terminate batch file which is running in background
我正在創建調用我的批處理文件的簡單Windows服務,現在我想通過將其停止到services.msc來停止或終止該批處理文件,我的服務已停止但該批處理文件在后台仍然處於活動狀態。如何終止批處理文件在后台我將停止我的服務。
先感謝您。
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
namespace Myservices
{
public partial class Myservices: ServiceBase
{
private ProcessStartInfo processListener;
Process p;
public Myservices()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
processListener = new ProcessStartInfo{
CreateNoWindow = true,
UseShellExecute = false,
FileName = "cmd.exe",
Arguments = string.Format("/C \"{0}\"", "C:\\myservices\\servceupdate.bat"),
WindowStyle = ProcessWindowStyle.Hidden,
RedirectStandardOutput = true,
RedirectStandardError = true,
ErrorDialog = false
};
p=Process.Start(processListener);
}
protected override void OnStop()
{
p.Close();//Here my batch file did not terminate.
}
}
在批處理文件servceupdate.bat
放一個標題,如下所示:
title servceupdate.bat
然后,在您要用於終止進程的腳本中,可以運行以下cmd:
for /f "tokens=2 delims= " %%a in ('tasklist /v ^| findstr "servceupdate.bat"') do taskkill /f /pid %%a
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.