Note that there are some explanatory texts on larger screens.

plurals
  1. POPausing a new BackGroundWorker until previous completes
    primarykey
    data
    text
    <p>I am struggling with threading. The problem is when I am iterating trough foreach loop.</p> <p>When setting <code>this.Document</code>, the application performs login, that is triggered with an event and takes few seconds to complete. In the <code>worker_RunWorkerCompleted</code> method I need to perform some actions that depend on current login information.</p> <p>The problem is that before I can perform this action for the first file, the <code>this.Document</code> already changes making the application perform another login. This way I can never actually perform my actions.</p> <p>My question is: How can I pause the next thread until previous thread has completed. Is there any other solution to my problem?</p> <p>I tried with <code>AutoResetEvent</code> but I got no luck. I set <code>waitOne()</code> just after the RunWorkerAsync call and <code>.Set()</code> in the RunWorkerCompleted. The code never gets to <code>RunWorkerCompleted</code>...</p> <p>Here is the code:</p> <pre><code> public void Start(object obj) { try { foreach (KeyValuePair&lt;string, Stream&gt; pair in this.CollectionOfFiles) { Worker = new BackgroundWorker(); Worker.DoWork += new DoWorkEventHandler(worker_DoWork); Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); using (Stream stream = pair.Value) { primaryDocument = new Document(stream); DataHolderClass dataHolder = new DataHolderClass(); dataHolder.FileName = pair.Key; dataHolder.Doc = secondaryDocument; //background thread call Worker.RunWorkerAsync(dataHolder); } } } catch (Exception ex) { // exception logic } finally { // complete logic } } private void worker_DoWork(object sender, DoWorkEventArgs e) { DataHolderClass dataHolder = ((DataHolderClass)e.Argument); // setting this attribute triggers execution of login event this.Document = dataHolder.Doc; e.Result = (dataHolder); } private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { // here I need to perform some actions that are depending on the current login DataHolderClass dataHolder = ((DataHolderClass)e.Result); this.eventAggregator.GetEvent&lt;ActionEvent&gt;().Publish(new Message(EMessageType.Info) { Title = dataHolder.FileName }); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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