2011-06-07 63 views
2

要么我抛出异常或自然引起它的DoWork它未捕获和控制不传递给工人完成例程不是得到那个异常,我在Program.cs中捕获从程序为什么在BackgroundWorker DoWork例程中未捕获异常?

文件

Type: System.Reflection.TargetInvocationException 
Source: mscorlib 
Message: Exception has been thrown by the target of an invocation. 
Target Site: System.Object _InvokeMethodFast(System.IRuntimeMethodInfo, System.Object, System.Object[], System.SignatureStruct ByRef, System.Reflection.MethodAttributes, System.RuntimeType) 
Module Name: mscorlib.dll 
Module Path: C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll 
Stack: 
    at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) 
    at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner) 
    at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) 
    at System.Delegate.DynamicInvokeImpl(Object[] args) 
    at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme) 
    at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj) 
    at System.Threading.ExecutionContext.runTryCode(Object userData) 
    at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme) 
    at System.Windows.Forms.Control.InvokeMarshaledCallbacks() 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.Run(Form mainForm) 

代码段

// ... 
BackgroundWorker worker = new BackgroundWorker();     
worker.WorkerSupportsCancellation = true; 
worker.WorkerReportsProgress = true; 
worker.DoWork += new DoWorkEventHandler(worker_DoWork); 
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged); 
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);     
worker.RunWorkerAsync(); 
// ... 

void worker_DoWork(object sender, DoWorkEventArgs e) 
{ 
    // ... 
    throw new Exception("I want to be caught and passed to completed event handler") 
    // ... 
} 
+0

请给我们一个如何使用'BackgroundWorker'的例子 – 2011-06-07 17:45:51

+0

这种异常通常有'InnerException',你能否也提供它呢? – Snowbear 2011-06-07 18:34:38

+0

没有InnerExceptions,我只是把它手动 – 9ine 2011-06-07 18:40:46

回答

0

您还可以尝试在worker_DoWork中发现您的异常。这样它肯定不会在其他地方传播。