6
併發的同事。來自後臺線程的陷阱異常
我需要能夠捕獲可能從後臺線程拋出的異常。
讓我們爲自己的代碼講(這是一個糟糕的代碼)
public delegate bool CheckForUpdatesHandler(Uri uri);
public class UpdatesChecker {
public event AsyncCompletedEventHandler CheckForUpdatesAsyncCompleted;
protected virtual void OnCheckForUpdatesAsyncCompleted(AsyncCompletedEventArgs args) {
if (CheckForUpdatesAsyncCompleted != null)
CheckForUpdatesAsyncCompleted(this, args);
}
public bool CheckForUpdates(Uri ftp) {
Thread.Sleep(1000);
throw new Exception("bla");
return true;
}
public void CheckForUpdatesAsync(Uri ftp){
var action = new CheckForUpdatesHandler(CheckForUpdates);
var c=action.BeginInvoke(ftp, delegate(IAsyncResult state) {
OnCheckForUpdatesAsyncCompleted(new AsyncCompletedEventArgs(null, false, null));
}, null);
}
}
謝謝,馬克,你再次幫助我:-) – Valentin 2009-02-10 13:21:42