404 Not Found
404 Not Found

Reputation: 3665

C# Asynchronous work within a BackgroundWorker?

I'm not the best with multi-threading, and this is a bit complicated, so hopefully I can explain it well.

Basically, what I have is a BackgroundWorker task, and inside it are several calls to retrieve information from a website. There may be a better way to do this, but I can't think of one. The reason I'm doing this is because I want to have a single event for when all of the calls to the website (and the subsequent processing of the information) are completed.

This is basically what I have going on:

private void loadContent() {
    BackgroundWorker worker = new BackgroundWorker();
    worker.DoWork += delegate(object sender, DoWorkEventArgs e) {
        getAndProcessInformation(desiredInformationName);
        getAndProcessInformation(someOtherDesiredInformationName);
    };
    worker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e) {
        MessageBox.Show("Finished!");
        Dispatcher.BeginInvoke(() => { progressBar(false); });
    };
    worker.RunWorkerAsync();
}
private void getAndProcessInformation(string theInformationIWant){
    Fetcher dataFetcher = new Fetcher(); //Fetcher is a custom class that reads information from a web server
    dataFetch.receivedData += delegate(DataResponseArgs args) {
        //Process some data
    };
    dataFetch.requestData(theInformationIWant);
}

So, what happens is that the RunWorkerCompleted is called right after the two getAndProcessInformation methods are called, and before the data is actually finished processing. Is there any way I can make RunWorkerCompleted only run once the processing in the methods is actually finished?

Now, I can see that the BackgroundWorker isn't really necessary, since the processing is actually already being done in a separate thread, but I don't know any alternative that would let me get the desired result of having one event triggered when all of the processing has been completed. So, I guess this is really two questions; Should I continue using the BackgroundWorker, and, if so, how do I fix it, or, if not, what's the alternative?

Upvotes: 2

Views: 1675

Answers (1)

parapura rajkumar
parapura rajkumar

Reputation: 24403

If your Fetcher is already creating its own thread, Using BackgroundWorker doesn't make much of a sense like you already figured out.

As you have not shared the implementation of Fetcher. It should probably be something like this if you want to stick with what you already have

private void getAndProcessInformation(string theInformationIWant){
    Fetcher dataFetcher = new Fetcher(); 
    var eventHandle =  new ManualResetEvent(false);
    dataFetch.receivedData += delegate(DataResponseArgs args) {
        //Process some data
        eventHandle.Set();
    };
    dataFetch.requestData(theInformationIWant);
    eventHandle.WaitOne();
}

This way the function getAndProcessInformation wont return until processing has been done. Only one Fetch will be working at a time.

If you want all Fetchers to work in parallel, you have remove the WaitOne and have the getAndProcessInformation return the wait handle. Then in DoWork you can do one wait for all handles to be signalled.

Upvotes: 1

Related Questions