0

My code looks like this:

    async public Task Btn(int pts)
    {
        switch (Settings.mode)
        {
            case MO.Assign:
            case MO.Learn:
                break;
            case MO.Quiz:
                await Task.Run(() => App.DB.IncrementPoints(phrase, 1));
                await Task.Delay(250);
                break;
            case MO.Practice:
                Device.BeginInvokeOnMainThread(() =>
                {
                    vm.Points = new String('☆', phrase.Points + pts);
                });
                await Task.Run(() =>
                {
                    App.DB.IncrementPoints(phrase, pts);
                    App.DB.IncrementHistory(HIST.Views);
                });
                await Task.Delay(250);
                break;
        }
        App.selectedPhrases = null;
        timer2Seconds = 2;
    }


    public void IncrementPoints(Phrase phrase, int pts)
    {
        lock (l)
        {
            db2.Execute("UPDATE phrase SET Points = Points + " + pts +
                        " WHERE PhraseId = '" + phrase.PhraseId + "'");
        }
    }

I am a bit confused with Task.Run and await. First of all I want this to update the screen:

vm.Points = new String('☆', phrase.Points + pts);

Then I want the database to be updated and then there to be a delay of 250ms.

But is it okay that my method IncrementPoints is not an async method?

8
  • My reply to a comment just removed: Thanks for your input. I was using it because I thought it may be the best way to do it but based on what you just said then maybe it's overkill. Is there any overhead or any issues with doing it with Task.Run? If you were to be doing the coding would you just call it without the await and Task.Run? Note that I have about 15,000 rows in the Points table but I do have an index on PhraseId. Commented Dec 31, 2018 at 7:58
  • 1
    When you call (await) this from the main thread then the BeginInvokeOnMainThread() should not be needed. Commented Dec 31, 2018 at 8:08
  • 5
    The best solution would be when App.DB has async methods on offer: remove Task.Run althogether and await the I/O. Commented Dec 31, 2018 at 8:09
  • 1
    And why do you lock() inside that method? Commented Dec 31, 2018 at 8:10
  • 2
    As far as the BeginInvoke goes, it depends on how you call this. Show the full call stack, abbreviated. The toplevel should be an async void event handler. The answer from sweepr below has the right idea. Commented Dec 31, 2018 at 8:26

1 Answer 1

3

I am not exactly sure what you want to do, but to answer your question:

But is it okay that my method IncrementPoints is not an async method?

Yes, it's ok, and that is also exactly one of the purposes of Task.Run. You can use Task.Run to turn synchronous operations to awaitable async operations. So you are supposed to put non-async methods in the lambda. If IncrementPoints were async, you wouldn't need Task.Run, because you can just do:

await IncrementPoints(...);

Your code should just be something like this:

vm.Points = new String('☆', phrase.Points + pts);

await Task.Run(() =>
{
    App.DB.IncrementPoints(phrase, pts);
    App.DB.IncrementHistory(HIST.Views);
});
await Task.Delay(250);
Sign up to request clarification or add additional context in comments.

Comments

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.