1

I'm working on a project and I need some help. Here's a code snippet:

private void MassInvoiceExecuted()
{
    foreach (Invoice invoice in Invoices)
    {
        DoStuff(invoice);
        //I'd like to wait 8 seconds here before the next iteration
    }

    RefreshExecuted();
}

How can this be done easily? What I've tried is:

private async void MassInvoiceExecuted()
{
    foreach (Invoice invoice in Invoices)
    {
        DoStuff(invoice);
        await Task.Delay(8000);
    }

    RefreshExecuted();
}

And although it did wait 8 seconds without freezing the UI it also waited for about 30 seconds right before RefreshExecuted(); I'm obviously not familiar with async await but it seemed like a good idea.

Anyway, I need to wait 8 seconds after each iteration without blocking the UI because I have to be able to abort the loop via button click. I've considered timers. Setting up the interval to 8000 and creating a tick method containing what exactly? I can't put everything that's in MassInvoiceExecuted inside the tick method because that wouldn't be right.

Any help would be much appreciated. Thanks!

3
  • 1
    What the problem with the async solution? It's fine for what i can see. Commented Sep 14, 2013 at 17:28
  • @AlessandroD'Andria It waits 8 secs which is ok. But also waits a whole lot more at the end before the effects of RefreshExecuted() are apparent in the UI. I'm guessing this somehow creates multiple threads which wait for each other or I don't know. Commented Sep 14, 2013 at 17:31
  • Well, why not simply move the method inside the loop, ok it's called many times, but at least every time an invoice it's processed your UI get updated. Commented Sep 14, 2013 at 17:35

2 Answers 2

2

For what i understand of your answer maybe you want

private async void MassInvoiceExecuted()
{     
    foreach (Invoice invoice in Invoices)
    {
        DoStuff(invoice);
        RefreshExecuted();
        await Task.Delay(8000);
    }
}

But really don't know if you have any reason to update UI only at the end of all processing.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm gonna mark this as an answer because it led me to find the correct way to do this. I couldn't use RefreshExecuted() multiple times because it connected to and updated and downloaded data from a database too many times which was not handy. But the key was inside RefreshExecuted() which I did not post, I know, because I thought it was irrelevant. Well it wasn't. Anyway, thank you for the idea, it helped me solve this!
0

The MassInvoiceExecuted method will return control to the UI thread as soon as it hits the await keyword. So your MassInvoiceExecuted method still runs and waits 8 seconds per invoice, I presume you have 4 invoices to process....

1 Comment

Well it has to wait 8 seconds per invoice but I don't see why it has to wait a lot more at the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.