早上好,在我的应用中,我是这样使用一个特定的录音软件的:
//OLD CODE
ProcessStartInfo start = new ProcessStartInfo();
start.Arguments = arguments;
start.FileName = "PROGRAM FOR RECORDING AUDIO";
start.WindowStyle = ProcessWindowStyle.Normal;
start.CreateNoWindow = true;
//use timer
runTimer();
using (Process proc = Process.Start(start))
{
proc.WaitForExit();
}
//Create mp3 and other operations
work();当我退出这个程序时,我的应用程序会创建mp3并执行其他操作。在录制过程中,此程序每分钟创建一次文件,并以日期和时间命名。我想要更新应用程序表单中的列表框,添加新创建的mp3文件的名称。为此,我使用计时器:
public void runTimer()
{
aTimer.Elapsed += new ElapsedEventHandler(RunEvent);
aTimer.Interval = 10000;
aTimer.Enabled = true;*/
int timeout = Timeout.Infinite;
int interval = 10000;
TimerCallback callback = new TimerCallback(RunEvent);
System.Threading.Timer timer = new System.Threading.Timer(callback, null, timeout, interval);
timer.Change(0, 10000);
}
public void RunEvent(object state)
{
//search file and update listbox
}但是列表框只有在音频录制软件退出时才会更新。我用以下代码更改了旧代码:
//TEST
Process pr = new Process();
ProcessStartInfo prs = new ProcessStartInfo();
prs.FileName = "PROGRAM FOR RECORDING AUDIO";
pr.StartInfo = prs;
ThreadStart ths = new ThreadStart(delegate() { pr.Start(); });
Thread th = new Thread(ths);
th.Start();这样,列表框就会正确更新。但是,我不知道如何处理音频录制软件闭包,以便使用我的旧代码中提供的work()方法。为我糟糕的英语道歉;)
发布于 2016-05-16 18:40:53
您可以使用异步操作。例如:
//use timer
//runTimer(); //Not needed now
Task.Factory.StartNew(() => {
using (Process proc = Process.Start(start))
{
proc.WaitForExit();
}
work(); //If you need to wait the process to finish
});
work(); //If you don't need to wait the process to finishhttps://stackoverflow.com/questions/37251580
复制相似问题