【发布时间】:2009-02-10 13:16:38
【问题描述】:
并发同事。
我需要能够捕获可能从后台线程抛出的异常。
让代码自己说话(这是一个糟糕的代码)
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);
}
}
【问题讨论】:
标签: c# multithreading