使用 PyInstaller 创建的可执行文件退出时如何通知 C# 代码?
How to inform C# code when executable created using PyInstaller exits?
linux 的类似问题已得到解答,但我找不到 windows 的答案。
我有一个 Python 脚本如下:
import sys
import os
#do something
# sys.exit(0) used this one, as well
os._exit(0)
我使用 PyInstaller 将其转换为 windows 可执行文件。
而 C# 代码是:
var process = new System.Diagnostics.Process();
process.StartInfo = new System.Diagnostics.ProcessStartInfo(path, "Long Argument");
process.Exited += (s, e) => { Console.WriteLine("Process Finished"); }; ;
process.Start();
process.WaitForExit();
进程启动并正确运行。但是当它退出时 Exited
事件不会被引发。
我也不确定我在哪一边做错了,C# 还是 python.
您需要在此处解释 Process.Exited 的活动 Process.Exited Event
根据您缺少的示例的编码部分 EnableRaisingEvents
应设置为 true
private Process myProcess = new Process();
private int elapsedTime;
private bool eventHandled;
// Print a file with any known extension.
public void PrintDoc(string fileName)
{
elapsedTime = 0;
eventHandled = false;
try
{
// Start a process to print a file and raise an event when done.
myProcess.StartInfo.FileName = fileName;
myProcess.StartInfo.Verb = "Print";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.Start();
}
catch (Exception ex)
{
Console.WriteLine("An error occurred trying to print \"{0}\":" + "\n" + ex.Message, fileName);
return;
}
// Wait for Exited event, but not more than 30 seconds.
const int SLEEP_AMOUNT = 100;
while (!eventHandled)
{
elapsedTime += SLEEP_AMOUNT;
if (elapsedTime > 30000)
{
break;
}
Thread.Sleep(SLEEP_AMOUNT);
}
}
linux 的类似问题已得到解答,但我找不到 windows 的答案。 我有一个 Python 脚本如下:
import sys
import os
#do something
# sys.exit(0) used this one, as well
os._exit(0)
我使用 PyInstaller 将其转换为 windows 可执行文件。 而 C# 代码是:
var process = new System.Diagnostics.Process();
process.StartInfo = new System.Diagnostics.ProcessStartInfo(path, "Long Argument");
process.Exited += (s, e) => { Console.WriteLine("Process Finished"); }; ;
process.Start();
process.WaitForExit();
进程启动并正确运行。但是当它退出时 Exited
事件不会被引发。
我也不确定我在哪一边做错了,C# 还是 python.
您需要在此处解释 Process.Exited 的活动 Process.Exited Event
根据您缺少的示例的编码部分 EnableRaisingEvents
应设置为 true
private Process myProcess = new Process();
private int elapsedTime;
private bool eventHandled;
// Print a file with any known extension.
public void PrintDoc(string fileName)
{
elapsedTime = 0;
eventHandled = false;
try
{
// Start a process to print a file and raise an event when done.
myProcess.StartInfo.FileName = fileName;
myProcess.StartInfo.Verb = "Print";
myProcess.StartInfo.CreateNoWindow = true;
myProcess.EnableRaisingEvents = true;
myProcess.Exited += new EventHandler(myProcess_Exited);
myProcess.Start();
}
catch (Exception ex)
{
Console.WriteLine("An error occurred trying to print \"{0}\":" + "\n" + ex.Message, fileName);
return;
}
// Wait for Exited event, but not more than 30 seconds.
const int SLEEP_AMOUNT = 100;
while (!eventHandled)
{
elapsedTime += SLEEP_AMOUNT;
if (elapsedTime > 30000)
{
break;
}
Thread.Sleep(SLEEP_AMOUNT);
}
}