Waitforexit统一中断正在运行的应用程序 [英] Waitforexit interrupt my running application in unity
问题描述
我正在尝试统一运行exe应用程序以执行某些功能,并且统一运行时该exe文件会从我的麦克风接收输入,因此我必须等到它退出后再使用waitforexit很好,允许exe接受输入但这不是很好,因为在exe运行过程中我的统一应用程序停止运行,直到我的exe完成为止,我想在我的exe运行时统一执行其他操作.
这是我的代码:-
System.Diagnostics.Process p =新的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();
您不必使用WaitForExit
,因为它阻塞了主线程.对于您的问题,有两种解决方法:
1 .将EnableRaisingEvents
设置为true
.订阅 Update
函数中它是否仍处于打开状态.
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 .继续您的WaitForExit
,但只能在新的Thread
中使用该代码,以免阻塞或冻结Unity的主线程./p>
//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();
请注意,您不能通过此新线程使用Unity的API.如果要这样做,请使用UnityThread.executeInUpdate
.参见这以获取更多信息.
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();
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.
这篇关于Waitforexit统一中断正在运行的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!