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?
lock()inside that method?async voidevent handler. The answer from sweepr below has the right idea.