0

So I have a background worker which carries out a task and I need the program to wait for this task to be completed before continuing, at the moment I'm doing this:

while(backgroundwork.IsBusy)
{
    Thread.Sleep(10);
}

however when I run this the program just stops running, seems like the background worker is running constantly but it isn't, there's no looping in the worker and it's finished its tasks before.

what could I have missed?

andrew
  • 125
  • 2
  • 6
  • is anything stopping you to use background worker completed event? – tabby Dec 29 '17 at 13:26
  • might be helpful: https://stackoverflow.com/questions/15316613/when-should-taskcompletionsourcet-be-used – Dmitry Pavliv Dec 29 '17 at 13:27
  • 1
    `Thread.Sleep` will cause the current thread (i.e. the foreground thread) to sleep. While it's sleeping, it ain't doing anything else. Show your code for how you create the background worker. – Matt Burland Dec 29 '17 at 13:27
  • 5
    The whole point of the background worker is that the UI thread **does not wait for it to complete**. If you do that, you might just as well call the code directly instead of through a background worker. The correct way to use a background worker is to subscribe to the `RunWorkerCompleted` event and do whatever needs to be done in the UI after the task has finished **there**. – Heinzi Dec 29 '17 at 13:29
  • 1
    @Heinzi that commment could be the answer in my opinion. Although you cannot really answer an incomplete question :( – Tobias Theel Dec 29 '17 at 13:31
  • As pointed out in a few comments, `BackgroundWorker` looks a bit fishy :) It's obsolete in current version of C# since you have more flexible async patterns at your disposal - maybe point out what you want to achieve, and we can come up with a better solution? – Philipp Sumi Dec 29 '17 at 13:31
  • 1
    Please read how to create a [minimal, complete, verifiable example](https://stackoverflow.com/help/mcve) and add the missing information to your Post by editing it :) If you haven't read [how to ask](https://stackoverflow.com/help/how-to-ask) yet i recommend to do so :) I highly recommend to follow the 2 guides i linked as the people on SO are more likely to answer questions when the posts follow these guides. What i'm missing in particular is - What have u tried so far? What errors/problems do you face? Do you may have codesnippets that help understand your question? Welcome to StackOverflow – Tobias Theel Dec 29 '17 at 13:33
  • The real issue here is why you want to 'wait', and what that should mean. Do you want to block certain UI features? etc. – H H Dec 29 '17 at 15:30
  • I just wanted to make sure that my progress bar wasn't empty until the task was completed. I just added another update before my task started to paint the bar halfway. All good now. Thank you – JianYA Dec 12 '18 at 00:04

3 Answers3

1

You can use BackgroundWorker.RunWorkerCompleted event. You can wait on a WaitHandle (like autoresetevent), and signal that from RunWorkerCompleted event.

Tilak
  • 30,108
  • 19
  • 83
  • 131
1

Thread.Sleep is used to wait for specified time and do nothing.

Async wait is used to wait until given task gets completed.

myMethod().wait() - here myMethod would be your async method and wait() is c# keyword which will wait asynchronous for that method to be completed.

see the difference between thread.sleep() and Async delay - Here!!

Ajay Kumar Oad
  • 560
  • 5
  • 15
0

Solution:

As Heinzi stated in a comment, you should just use the RunWorkerCompleted event.

Here is an good example on "how to use the backgroundworker runworkercompleted event" You shall never "wait" for a task/thread to finish, but you could "wait" for waithandles or let the thread do other stuff in the meantime

In the Class where you hold an instance of the Backgroundworker you can simply attach to the completed event and your fine.

Delete that loop with the Thread.Sleep() in it

.

private void WorkerExample() {
    BackgroundWorker bw = new BackgroundWorker();
    bw.RunWorkerCompleted += OnWorkerCompleted; //listen for that event by attaching a handler
    bw.RunWorkerAsync();
}

//This is the EventHandler
private void OnWorkerCompleted(object sender, RunWorkerCompletedEventArgs runWorkerCompletedEventArgs) {
    //This method is called, when the worker finished his job
}

Further Information:

There are also some more useful events on the BackgroundWorker.

Backgroundworker events

MSDN provides a complete example how to use the BackgroundWorker. You should have a look at that.

Tobias Theel
  • 3,088
  • 2
  • 25
  • 45