I fight streams. The problem is that I am repeating the foreach loop.
When setting up, the this.Documentapplication performs a login, which starts with an event and takes a few seconds. In the method, worker_RunWorkerCompletedI need to perform some actions depending on the current login information.
The problem is that before I can perform this action for the first file, I this.Documentalready changed the application execution for another input. Thus, I can never carry out my actions.
My question is: how can I pause the next thread until the previous thread completes. Is there any other solution to my problem?
I tried with AutoResetEventbut I had no luck. I installed waitOne()right after calling RunWorkerAsync and .Set()in RunWorkerCompleted. The code never gets into RunWorkerCompleted...
Here is the code:
public void Start(object obj)
{
try
{
foreach (KeyValuePair<string, Stream> 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;
Worker.RunWorkerAsync(dataHolder);
}
}
}
catch (Exception ex)
{
}
finally
{
}
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
DataHolderClass dataHolder = ((DataHolderClass)e.Argument);
this.Document = dataHolder.Doc;
e.Result = (dataHolder);
}
private void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
DataHolderClass dataHolder = ((DataHolderClass)e.Result);
this.eventAggregator.GetEvent<ActionEvent>().Publish(new Message(EMessageType.Info) { Title = dataHolder.FileName });
}
source
share