1 回答

TA貢獻1860條經驗 獲得超8個贊
您可以通過在傳遞給方法的操作中添加 try/catch 語句來捕獲 UI 線程上的異常Invoke:
private void UpdateUI()
{
Application.Current.Dispatcher.Invoke(() =>
{
try
{
if (SecurityComponent.CurrentLoggedUser != null)
{
var user = SecurityComponent.CurrentLoggedUser;
m_userLabel.Text = user.Username + " - " + user.Role.Name;
}
UpdateTerritories();
ViewsIntegrationService.SetHmiMode(EHmiModes.Normal);
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
}
});
}
如果將 try/catch 放在對Invoke方法的調用周圍,則可以在后臺線程上處理異常。將它放在可能實際拋出的實際代碼周圍更有意義。
- 1 回答
- 0 關注
- 659 瀏覽
添加回答
舉報