这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
for(i = 0; i < 500; i++)
{
CallingTailorCode;
getvalue();
WriteData();
}
}
public void getvalue()
{
string[] lines = File.ReadAllLines("C:\\Users\\bryan\\Desktop\\generationfile.txt");
foreach (string line in lines)
{
notepadvalue = line;
}
}
private void CallingTailorCode()
{
Process callcfunction = new Process();
callcfunction.StartInfo.UseShellExecute = false;
callcfunction.StartInfo.FileName = "C:\\Users\\bryan\\Desktop\\ConsoleApplication1\\Debug\\ConsoleApplication1.exe";
callcfunction.Start();
Thread.Sleep(1000);
}
public void WriteData()
{
Excel excel = new Excel("C:\\Users\\bryan\\Desktop\\SphereConvRws.xlsx", 1);
excel.WriteToCell(i, 0, notepadvalue);
excel.Save();
excel.close();
Thread.Sleep(1000);
}
CallingTailorCode()
是 c++ 和代码,我将输出存储在 .txt
文件中。
getValue()
我在其中读取 .txt
文件并将其存储为变量。
最后,WriteData()
我将值存储在 Excel 工作表中。
如果我将循环设置为 (i < 10) ,则不会出现错误,但如果我将循环设置为较大的数字,例如 500 或 1000 ,则会出现错误,例如 generationfile.txt
正在被其他进程使用。我想知道这个问题是否与程序不是异步有关?我应该如何使这段代码异步?
我为 CallingTailorCode
设置线程(1000),因为它是一个遗传算法代码,让它运行,我不确定我是否做对了
最佳答案
我应该使用 proccess.WaitForExit() 而不是使用 thread.sleep 。它使程序运行得更快,循环内的方法有条不紊地运行。
关于c# - 运行多个进程、C++ 和 Excel 导致的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53113970/