我有以下代码:
while(condition == true){ //第一部分 使用(var stream = File.Create(audioPath)) { 使用(WaveFileWriter writer = new WaveFileWriter(stream,…
我不知道这是否会有所帮助,但你可以尝试摆脱一个 using 并使用其他构造函数 WaveFileWriter :
using
WaveFileWriter
using (WaveFileWriter writer = new WaveFileWriter(fileName, waveFormat)) { writer.WriteData(testSequence, 0, testSequence.Length); }
你真的在生成文件吗? 相同 名称? audioPath 似乎没有改变。
audioPath
while (condition == true) { //First part using (var stream = File.Create(audioPath)) { using (WaveFileWriter writer = new WaveFileWriter(stream, waveFormat)) { writer.Write(audioBytes.ToArray(), 0, audioBytes.ToArray().Length); } } //Second part using (Process.Start("cmd.exe", commands)) { }; Thread.Sleep(1000); }
考虑以下:
auditPath
cmd
或者
Process.Start()
总而言之,你在这里遇到了竞争条件。确保你等待 Process 完成, 例如
Process
using (var proc = Process.Start(...)) { proc.WaitForExit(); // You might want to check `proc.ExitCode`, etc. }
在运行下一个循环周期之前。
关键要点:Process.Start()不是同步的。如果您需要等待已启动的命令完成,则需要明确地执行此操作,否则它将继续在后台运行并可能会干扰您的其他逻辑 - 就像目前一样。