由于项目中需要实现CPU和GPU的并行计算处理,而考虑到这些功能在python代码中较为容易实现,因此将Python代码封装后再进行调用。Python代码的封装可以参考:
【Python可执行文件生成】使用pyinstaller对多个文件的Python项目生成可执行(.exe)文件_python创建多个文件模板不同的执行器_EISHIN_NI的博客-CSDN博客
调用代码见下:
string img_path = Open.FilePaths[0];
string exe_path = root_path + "\\cuda_test.exe";
if (bandInt > 1000) Tt = 0.37986514;
string[] the_args = { bandInt.ToString(), UAVheight.ToString(), solarAltitudeAngle.ToString(), temperature.ToString(), img_path.ToString(), train_path.ToString(), after_train_path.ToString(), canshu_path.ToString(), trainornot.ToString(), Tt.ToString() }; // 被调exe接受的参数
StartProcess(exe_path, the_args);
CNNstep = 1;
while (System.Diagnostics.Process.GetProcessesByName("cuda_test").ToList().Count > 0)
Thread.Sleep(1000);
其中Startprocess函数具体实现如下:
public bool StartProcess(string runFilePath, params string[] args)
{
string s = "";
foreach (string arg in args)
{
s = s + arg + " ";
}
s = s.Trim();
Process process = new Process();//创建进程对象
ProcessStartInfo startInfo = new ProcessStartInfo(runFilePath, s); // 括号里是(程序名,参数)
process.StartInfo = startInfo;
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
process.Start();
//if (System.Diagnostics.Process.GetProcessesByName("main").ToList().Count > 0){
// int a = 0;
// }
// else {
// int b = 0;
//}
return true;
}