Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing custom object to RunWorkerCompleted event in case of an exception
    primarykey
    data
    text
    <p>My Console App uses <code>System.ComponentModel.BackgroundWorker</code> for threading purposes:</p> <pre><code>System.ComponentModel.BackgroundWorker backgroundWorker = new System.ComponentModel.BackgroundWorker(); backgroundWorker.DoWork += (sender, e) =&gt; ReportStatus(worker, status, result, e); backgroundWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); backgroundWorker.RunWorkerAsync(worker); </code></pre> <p>As you can see that I am passing "worker" as an argument inside RunWorkerAsync.</p> <p>What I am trying to achieve is that if there is an exception inside ReportStatus method I need the same "worker" object so that I can perform some operation (Call a service to notify that workers exception)</p> <pre><code>private void ReportStatus(Worker worker, Status status, WorkResult result,System.ComponentModel.DoWorkEventArgs arg) { var proxy = new PreparationServiceProxy(new NetTcpBinding(), new EndpointAddress(PreparationEngineState.ServiceAddress)); try { proxy.ReportStatus(worker, status, result); proxy.Close(); } catch (Exception) { arg.Result = worker; proxy.Abort(); throw; } } </code></pre> <p>In my exception block (I am not sure if this is the correct way!) I am assigning the worker to the Result so that I can get the same worker back when the RunWorkerCompleted method (backgroundWorker1_RunWorkerCompleted) is executed :</p> <pre><code>private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e) { if (e.Error != null) { Worker worker = e.Result as Worker; // At this point I get an exception! } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload