Waitforexit 统一中断我正在运行的应用程序

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

问题描述

我正在尝试统一运行 exe 应用程序以执行某些功能,并且该 exe 文件在统一运行时将从我的麦克风获取输入,因此我必须等到它退出,而使用 waitforexit 允许 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.

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

上一篇:半透明PNG作为启动画面 下一篇:“带外"asp.net 应用程序的处理技术

相关文章

最新文章