2

I'm working in a WPF application where i'm making a save process inside a thread. After the save process i'm initiating the load process between that i need a delay so i used the Task.Delay method.

During the save/load a progress bar is shown in the application untill it completes. Now when i use the Task.Delay method for 2 seconds no progress bar is shown in the application.

Now the issue is the delay(2 seconds). When there is a delay the user starts using the application and suddenly the load process starts.

So i need to make the Task.Delay method to be inside a Thread so that i can show the progress bar for that delay too.

I tried the following code but it throws me a warning,

//ViewModel Code:

 BackgroundWorker bw;
 async System.Threading.Tasks.Task PutTaskDelay()  //warning here
        {
            bw = new BackgroundWorker();
            IsBusy = true;        // to show the progress bar
            bw.RunWorkerAsync();

            bw.DoWork += async (s, e) =>
            {
                await System.Threading.Tasks.Task.Delay(2000);
            };

            bw.RunWorkerCompleted += (s, e) =>
            {
                IsBusy = false;   // Hide the progress bar
            };
        }

Warning:

The async method lacks await operators and will run synchronously. Consider using the async operator to await non-blocking API calls, or await Task.Run(...) to do CPU-bound work on a backgrond thread.

If there is alternate method let me know.

A Coder
  • 3,039
  • 7
  • 58
  • 129
  • 2
    What is the purpose of the two-second delay? IMHO, if you've got a problem you think you've solved by the equivalent of `Thread.Sleep`, then you probably don't understand the problem. – moswald Mar 08 '16 at 12:24
  • @moswald: I need the delay after the thread completes its process. So i'm unable to use `Thread.Sleep`. May be i don't. Can you please share some modified code of mine so that i might be clear. – A Coder Mar 08 '16 at 12:27
  • 1
    That's what I mean, why do you say you need the delay after the thread completes it's process? – moswald Mar 08 '16 at 12:27
  • At times the load process gets initiated before the save completes. The delay works at that time. – A Coder Mar 08 '16 at 12:30
  • 3
    It sounds like you 1) start saving then 2) start loading, but they aren't tied together. To fix this, you 1) start saving, 2) start sleeping 2 seconds, then 3) start loading. This is a race condition that you're just making less likely, but you aren't actually fixing. You should rewrite your save/load logic so that the load doesn't start until the save is guaranteed to be finished. The `Task.Delay` might be working on your dev environment, but it's not guaranteed to work every time. – moswald Mar 08 '16 at 12:33
  • Yes, this is what i'm doing now. Is there anyway that i can rewirte the logic? Just an description is enough. Kindly help. – A Coder Mar 08 '16 at 12:35
  • 1
    You will need to edit your post to include more code to see how you do your saving and loading logic. We can't tell what you're doing just from the sleep method. – moswald Mar 08 '16 at 12:36
  • Will post it ASAP, thanks. – A Coder Mar 08 '16 at 12:43
  • @moswald : I have posted this as a new question. Kindly take a look. http://stackoverflow.com/questions/35884884/concurrent-multi-threading-c-sharp – A Coder Mar 09 '16 at 06:53
  • You should not mix background worker and async programming. It is much harder to understand what happen when you don't follow either model but do a mix of them. – Phil1970 Aug 18 '16 at 14:13

2 Answers2

1

BackgroundWorker is obsolete. Use async methods in combination with Task.Run instead and the problem goes away.

usr
  • 168,620
  • 35
  • 240
  • 369
0

Your first async isn't required because you don't use await. That's what the warning is telling you.

This is the equivalent to what you've originally posted, with a lot less work involved. Note that async is now required.

async Task PutTaskDelayAsync()   // convention is to name async methods with Async suffix
{
    IsBusy = true;
    await Task.Delay(2000);
    IsBusy = false;
}
moswald
  • 11,491
  • 7
  • 52
  • 78
  • This doesnt makes the progress bar come. Only when there is a `BackgroundWorker` running the progress bar arrives. – A Coder Mar 08 '16 at 12:32