2016-05-16 44 views
1

早上好, 在我的應用我以這種方式使用特定的錄音軟件:C#的ThreadStart VS的ProcessStartInfo()

//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 
} 

但列表框更新時的錄音軟件quited。 我改變了舊代碼與以下之一:

//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()方法。對不起,我的英語不好;)

+0

您是否考慮過使用文件系統監視器? https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher%28v=vs.110%29.aspx – MikeT

回答

0

你可以使用異步操作。例如:

//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 finish