Waitforeexit 统一中断我正在运行的应用程序 [英] Waitforexit interrupt my running application in unity

查看:21
本文介绍了Waitforeexit 统一中断我正在运行的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试统一运行一个 exe 应用程序以执行某些功能,并且该 exe 文件在统一运行时将从我的麦克风获取输入,因此我必须等到它退出,同时使用 waitforeexit 可以很好地允许 exe 获取输入但这并不好,因为我的 exe 运行期间的统一应用程序停止,直到我的 exe 完成,我想在我的 exe 运行时统一执行其他操作.

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.

这是我的代码:-

System.Diagnostics.Process p = new System.Diagnostics.Process();

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();

推荐答案

您不必使用 WaitForExit,因为它会阻塞主线程.您的问题有两种解决方法:

You don't have to use WaitForExit since it's blocking the main Thread. There are two workarounds for your issue:

1.将 EnableRaisingEvents 设置为 true.订阅 Exited 事件并使用它来确定打开的程序何时关闭.在 Update 函数中使用布尔标志来确定它是否仍然打开.

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.继续使用您的 WaitForExit 只在新的 Thread 中使用该代码,这样它就不会不阻塞或冻结 Unity 的主线程.


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();

请注意,您不能从这个新线程使用 Unity 的 API.如果您想这样做,请使用 UnityThread.executeInUpdate.见 这个了解更多信息.

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.

这篇关于Waitforeexit 统一中断我正在运行的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆