c# - Backgroundworker : exception during cancellation -


I have a background worker who can be canceled.

In the middle of the normal flow only when the CancelPending variable is turned into true (UI which responds to the user interaction on the call worker.CancelAsynch ()), exception is thrown because of (that's Since the normal flow is interrupted, many Null Referee exceptions are thrown out)

So when the worker returns, I threw the exception (to silently ignore them) that when the worker canceled Given has been thrown Want to be able to do when the worker was not canceled (to report them on the UI)

My code is following (sorry for the c # / vb mix ...):

Worker class:

  Public Class ClassBaseGetObjectsWorker Inherited System.ComponentModel.BackgroundWorker protected override sub-OnDoWork (as ByVal e System.ComponentModel.DoWorkEventArgs) While CPT's try & lt dim CPT int16 as 0 = try; 5. System.Threading.Thread.Sleep (1000) CPT = CPT +1 then CheckForCancellation () then discard exception to the exception to the new exception ("Exception duo to cancel!") End if end Call Back:  
  Complete the zero set objettalkers@reconsultant (object sender, Run worker compliant event event e) {if (e. Cancellation) {resultLabel.Text = "cancel"; //e.Error has been completely ignored} and (if (E. error! = Null) {// E. error in reporting errors occurring during normal (autocratic) flow; InnerException;} Else { // The worker is ending or canceling the updateUIAfterBackgroundJob (); Then when I am doing the worker without the process error. Cancellation (), E. has been canceled, which is incorrect in full call back That I was not expected) If I comment on "Tro X" in the worker, if I do the test again, then the e cancellation has been set from right to right.  

So, the information I want What is the cleanest way to get it, that is: I want to know that the exception of being out in full handler was thrown when the worker was in cancellation status or not.

(My bad english Sorry for)

< Div class = "itemprop =" text ">

The best way to do the structure of the code in your OnDoWork () - Implementation is to throw an exception when you have the following functions to detect the cancellation :

Create a CancelException:

  Public class CancelException: Exception {}  

When you can find out that cancellation is pending Is: P>

  if (checker connection ()) new CancelException throw ();  

Add a try hold around the code in your OnDoWork () - Method:

  Protected override Zero OnDoWork (DoWorkEventArgs e) {try {//. ..} hold (exception to cancel) {// quietly return back; }}  

In this way, your code will follow the post-recurring contract (which, instead of throwing an exception, when you can find out the cancellation), and the cancellation properties are now You should expect

to be

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? -