Question by
Volatile_Potato · Sep 09, 2016 at 10:46 AM ·
processsystem.diagnostics
run an external exe if that specific exe is not running after sometime - run it
Hi all,
This is my attempt at detecting if a specified external exe is running or not and if it is not relaunch after 5 mins/300 seconds. However it spam launches infinitely. What am I doing wrong? Probably missed a silly little thing.
using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System.Threading;
public class Handler : MonoBehaviour {
public int reUpdate;
public int timetoUpdate;
public string ICEexe;
// Use this for initialization
void Start()
{
// OnStartup();
}
void Update()
{
if (Time.time > reUpdate) //7s
{
UnityEngine.Debug.Log("Checking if ICEexe is false");
OnStartup();
}
}
public bool IsProcessOpen(string name)
{
foreach (Process copProcess in Process.GetProcesses())
{
if (copProcess.ProcessName.Contains(ICEexe))
{
return true;
}
}
return false;
}
void OnStartup() // other apps on Start()
{
Process currentProcess = Process.GetCurrentProcess();
if (IsProcessOpen(ICEexe) == false)
{
UnityEngine.Debug.Log("ICE not running start coroutine");
StartCoroutine(RelaunchICE());
}
//return;
}
IEnumerator RelaunchICE()
{
UnityEngine.Debug.Log("Im in IEnumerator waiting for lapse time:" + timetoUpdate);
yield return new WaitForSeconds(timetoUpdate);
}
void launchICE()
{
try
{
UnityEngine.Debug.Log("Im in launchICE method, launching" + ICEexe);
Process.Start(ICEexe);
}
catch (System.Exception e)
{
print(e);
}
}
}
Comment
Answer by Volatile_Potato · Sep 09, 2016 at 01:17 PM
I fixed it. The following are code changes, there are two fixes to this solution.
using UnityEngine;
using System.Collections;
using System.Diagnostics;
using System.Threading;
public class Handler : MonoBehaviour {
public int reUpdate;
public int timetoUpdate;
public string ICEexe;
// Use this for initialization
void Start()
{
// Invoke("OnStartup", 6); << simple one line code does the trick
}
bool isStarted = false;
void Update()
{
//this is also another way
/* if (Time.realtimeSinceStartup > reUpdate && !isStarted) //7s
{
isStarted = true;
UnityEngine.Debug.Log("Checking if ICEexe is false");
OnStartup();
}*/
}
public bool IsProcessOpen(string name)
{
foreach (Process copProcess in Process.GetProcesses())
{
if (copProcess.ProcessName.Contains(ICEexe))
{
return true;
}
}
return false;
}
void OnStartup() // other apps on Start()
{
//Process currentProcess = Process.GetCurrentProcess();
if (IsProcessOpen(ICEexe) == false)
{
UnityEngine.Debug.Log("ICE not running start coroutine");
StartCoroutine(RelaunchICE());
}
if (IsProcessOpen(ICEexe) == true)
{
UnityEngine.Debug.Log("ICE is running");
}
//return;
}
IEnumerator RelaunchICE()
{
UnityEngine.Debug.Log("Im in IEnumerator waiting for lapse time:" + timetoUpdate);
yield return new WaitForSeconds(timetoUpdate);
launchICE();
}
void launchICE()
{
try
{
UnityEngine.Debug.Log("Im in launchICE method, launching" + ICEexe);
Process.Start(ICEexe);
}
catch (System.Exception e)
{
print(e);
}
}
}
Your answer
Follow this Question
Related Questions
Switch between two Unity applications 1 Answer
Can’t open project from usb as it says opening file failed 0 Answers
Using -parentHWND makes everything slow ..? 0 Answers
System.Daignostic. Stopwatch Not working on Builds but Works in Editor 2017.2.0p2 0 Answers
Unity debugger hangs when using System.Diagnostics.Process 0 Answers