Reputation: 99
I'm trying to run an exe application in unity to perform some function and that exe file will take an input from my microphone when running in unity so i must wait until it exit while using waitforexit is nice to allow exe takes input but it's not good because my unity app during the exe running becomes stopped till it my exe finish and i want perform somethings else in unity while my exe running.
this is my code :-
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo = new System.Diagnostics.ProcessStartInfo("E:\\app\\dist\\app.exe");
p.StartInfo.WorkingDirectory = @"\Assets\\app\\dist\\app.exe";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.Start();
p.WaitForExit();
Upvotes: 1
Views: 1036
Reputation: 99
Thanks guys but i found the final solution that works well here. it will save your time
https://www.youtube.com/watch?v=C5VhaxQWcpE
this my code after applying the solution :-
And this my code after applying the solution in the video above
public async void run_speechToText()
{
Task task = new Task(StartListening);
task.Start();
Debug.Log("My exe file is running right now");
await task;
get_answer();
}
public void StartListening()
{
p.StartInfo = new System.Diagnostics.ProcessStartInfo("E:\\app\\dist\\app.exe");
p.StartInfo.WorkingDirectory = @"\Assets\\app\\dist\\app.exe";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.Start();
p.WaitForExit();
}
Upvotes: 1
Reputation: 125455
You don't have to use WaitForExit
since it's blocking the main Thread. There are two workarounds for your issue:
1.Set EnableRaisingEvents
to true
. Subscribe to the Exited
event and use that to determine when the opened program has closed. Use a boolean flag to determine if it is still opened or not in the Update
function.
bool processing = false;
void Start()
{
processing = true;
Process p = new Process(); ;
p.StartInfo = new System.Diagnostics.ProcessStartInfo("E:\\app\\dist\\app.exe");
p.StartInfo.WorkingDirectory = @"\Assets\\app\\dist\\app.exe";
p.StartInfo.CreateNoWindow = true;
p.EnableRaisingEvents = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.Exited += new EventHandler(OnProcessExit);
p.Start();
}
private void OnProcessExit(object sender, EventArgs e)
{
processing = false;
}
void Update()
{
if (processing)
{
//Still processing. Keep reading....
}
}
2.Continue with your WaitForExit
but only use that code in a new Thread
so that it doesn't block or freeze Unity's main Thread.
//Create Thread
Thread thread = new Thread(delegate ()
{
//Execute in a new Thread
Process p = new Process(); ;
p.StartInfo = new System.Diagnostics.ProcessStartInfo("E:\\app\\dist\\app.exe");
p.StartInfo.WorkingDirectory = @"\Assets\\app\\dist\\app.exe";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.Start();
p.WaitForExit();
//....
});
//Start the Thread and execute the code inside it
thread.Start();
Note that you can't use Unity's API from this new Thread. If you want to do so, use UnityThread.executeInUpdate
. See this for more information.
Upvotes: 3