0
我正在編寫一個Xamarin Android應用程序,並且我有一個長時間運行的本地(Java)進程。我想捕獲進程的輸出(stdout,stderr)並用進度更新UI。我下面的代碼不起作用。現在它阻止了UI線程。Xamarin:從長時間運行的本機進程更新用戶界面
更新UI而不阻塞UI線程的正確方法是什麼?
string[] myCmd = { "unix_cmd", "--args" };
process = Runtime.GetRuntime().Exec(myCmd);
BufferedReader bufferedStdoutReader = new BufferedReader(new InputStreamReader(process.InputStream));
BufferedReader bufferedStderrReader = new BufferedReader(new InputStreamReader(process.ErrorStream));
logView.Text += "Stdout >>>>>>>>" + System.Environment.NewLine;
var txt="";
txt = bufferedStdoutReader.ReadLine();
while (txt != null)
{
logView.Text += txt + System.Environment.NewLine;
txt = bufferedStdoutReader.ReadLine();
}
logView.Text += "Stdout <<<<<<<<<" + System.Environment.NewLine + System.Environment.NewLine;
logView.Text += "Stderr >>>>>>>>>>" + System.Environment.NewLine;
txt = bufferedStderrReader.ReadLine();
while (txt != null)
{
logView.Text += txt + System.Environment.NewLine;
txt = bufferedStderrReader.ReadLine();
}
logView.Text += "Stderr <<<<<<<<<<" + System.Environment.NewLine;
process.WaitFor();
Better Ac tivity.RunOnUiThread(()=> /*....*/); – XTL
Activity.RunOnUiThread是否替換ApplicationSynchronizationContext.Post調用? – Rick
@瑞克,是的,可以被替換。 –