- Home /
Question by
NGC6543 · Jan 12, 2018 at 05:23 AM ·
processexternal-applicationsystem.diagnostics
System.Diagnostics.Process.Kill() doesn't work
Hi, I have a script that starts an external process on Unity. It runs as expected, but calling process.Kill() has no effect. The external app isn't being terminated.
public void InitSensor ()
{
sensorStatus = SensorStatus.Initializing;
pSensor = new System.Diagnostics.Process();
pSensor.StartInfo.FileName = Application.streamingAssetsPath + "/MyApp.app";
pSensor.StartInfo.CreateNoWindow = false;
pSensor.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
pSensor.EnableRaisingEvents = true;
pSensor.Exited += OnSensorTerminated;
pSensor.Start();
sensorStatus = SensorStatus.Running;
}
public void KillSensor()
{
Debug.Log("Killing Sensor...");
if (pSensor != null)
{
pSensor.Exited -= OnSensorTerminated;
try
{
pSensor.Kill();
}
catch (Exception e)
{
Debug.LogWarning(e);
}
finally
{
pSensor.Close();
pSensor = null;
sensorStatus = SensorStatus.Exited;
}
}
}
The code is as above. Am I missing something??
Comment
Answer by emicenizo · Jul 29, 2021 at 04:57 PM
I usually do this to kill processes on unity (remember that the process name doesn't necessarily mean the executable name):
using System.Diagnostics;
static void closeApp(string appProcessName) {
Process[] workers = Process.GetProcessesByName("your app name")
foreach (Process worker in workers)
{
worker.Kill();
worker.WaitForExit();
worker.Dispose();
}
}
Your answer
Follow this Question
Related Questions
Run Terminal Command using System.Diagnostics.Process not working on osx 2 Answers
Type 'System.Diagnostics.Process' does not contain a definition for 'CloseMainWindow' 1 Answer
How to get .exe file to run through script? 1 Answer
In build - Process cannot find file 1 Answer
Kill external process in Unity 2 Answers