c# - Unhandled exceptions in BackgroundWorker -


I have a small WinForms app that uses a background graphic object to perform long lasting tasks.

Background operations sometimes throw an exception, especially when a person's file is opened which is being rebuilt.

Whether the code is run by IDE or not. An error dialog pops up informing the net user is unrestricted exception, it is not changed either by compiling the code by using the release configuration. According to

:

If the operation raises an exception that your code does not handle, the BackgroundWorker catches the exception and passes it into a runawalker integrated event handler, Where it system The error is highlighted as the error property of the mod.module .. :: RunWorkerCompletedEventArgs. If you are operating under the Visual Studio Debugger, Debugger will break the point in the Dockwork event handler, where an unused exception was raised.

I hope that to throw these exceptions on the occasion and to handle them at the RunWorkerCompleted event instead of DoWork, my code works properly and the error is correct within the event of the RunWorker event. Is controlled in a manner, but I can not understand this for my life, to stop the .NET error dialog that complains about "unexpected exception".

Does the backgroundwire not automatically catch that error? Is not that what MSDN documentation is? What do I need to do to notify .NET that this error is is while still allowing the exception to runWorkerCompletedEventArgs in the error property?

The description that you are describing is not a defined behavior of the background type. You're doing something wrong, I doubt it.

Here is a small sample that proves that the background workers account exceptionally in DoWork , and they will give you RunWorkerCompleted :

  var worker = new backgroundwork (); Worker.DoWork + = (Sender, E) = & gt; {New Invalid Operation Expression ("Oh Shazenet!"); }; Worker.RunWorkerCompleted + = (Sender, E) = & gt; {If (e.Error! = Null) {MessageBox.Show ("was an error!" + E. Error. ToString ()); }}; Worker.RunWorkerAsync ();  

My mental debugging skills are exposing your problem to me: Are reaching out. Handlers included in your runover - if any e Error, you can get it e . For example, the following code is bad, bad, bad, and throws an exception to runtime:

  var worker = new BackgroundWorker (); Worker.DoWork + = (Sender, E) = & gt; {New Invalid Operation Expression ("Oh Shazenet!"); }; Worker.RunWorkerCompleted + = (Sender, E) = & gt; {// OH, NOOOOOOOES! Runtime exception, you can not access e.Result if you have any // error. You can check the errors using the error. Var results = E. result; }; Worker.RunWorkerAsync (); Here is a proper implementation of RunWorker's Compiled Event Handler:  
  Private Zero Runwalker Compatibility Handler (Object Sender, Runwalker, Collected Entente E) {if (e.Error == null) {Dosomingwith (E. Results); // Access E. The result was only if there was no error. }}  

Voila, you will not get runtime exception.


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -