Waitforexit 统一中断我正在运行的应用程序
- 作者: 不是公主命却得了公主病
- 来源: 51数据库
- 2023-02-08
问题描述
我正在尝试统一运行 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.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源