2011-03-29 108 views
1

我有收到由我的應用催生了一個小長時間運行控制檯應用程序控制臺的一些標準輸出的問題。入門從控制檯應用程序,標準輸出asyncronously無需等待控制檯退出

我的應用程序啓動控制檯應用程序和控制檯應用程序將活着聽我的應用程序生命週期內的端口上(或直到明確殺死)。

當控制檯應用程序啓動時,它輸出它正在監聽的端口號,我需要asyncronously抓住該端口號和應用程序的其他地方使用它。問題是,我的事件處理程序來獲取輸出數據永遠不會被調用。我相信一定有一些小事,我忘了做。

ProcessStartInfo si = new ProcessStartInfo(); 
    si.FileName = theFileName; 
    si.Arguments = theargs; 
    si.UseShellExecute = false; 
    si.RedirectStandardOutput = true; 
    si.CreateNoWindow = true; 
    _process = Process.Start(si); 
    _process.OutputDataReceived += (sender, args) => 
    { 
     //Parse the args.Data string for the port number. 
    }; 
    _process.BeginOutputReadLine(); // My handler never gets called. 
    // I don't want to call _process.WaitForExit() here as this is a long running process. 

回答

1

好的,在我的特殊情況下,問題是由控制檯應用程序不刷新標準輸出引起的。

控制檯應用程序是用Python編寫的(然後在Windows由py2exe製造可運行),它需要...

sys.stdout.flush() 

被稱爲之後,纔可以對之前退出應用程序得到任何輸出。