2016-05-16 10 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