Код: Выделить всё
private async Task ExecuteStrikes(List currentStrikes, bool isAdvice, CancellationToken token)
{
foreach (var strike in currentStrikes)
{
await CheckPauseAsync(token); // Kontrola pauzy
if (end || skipped || token.IsCancellationRequested) break;
await StopAll(token);
if (!skipped)
{
// Přímé volání metod bez zbytečného vytváření nových úloh.
switch (strike)
{
case PunchClass _:
await GenericSounds.PlayPunchSound(token);
break;
case DefenceClass _:
await Task.Delay(50, token);
await GenericSounds.PlayDefenceSound(token);
break;
case MoveClass _:
await Task.Delay(200, token);
await GenericSounds.PlayMoveSound(token);
break;
}
await Task.Delay(isAdvice ? 0 : Convert.ToInt32(300 / 1), token);
}
}
}
Код: Выделить всё
private async Task ExecuteCombo(ComboClass combo, bool isAdvice, CancellationToken token)
{
int currentRep = 0;
int adjKTime = Convert.ToInt32(Math.Round((double)combo.time / 1));
for (int i = 0; i < combo.reps; i++)
{
await CheckPauseAsync(token); // Check pause before each iteration
if (token.IsCancellationRequested) return;
currentRep++;
if (!isAdvice)
{
strike_label.BackgroundColor = Color.FromRgb(255, 0, 0);
await GenericSounds.PlayGoSound(token);
}
await Task.Delay(300, token);
ChangeTextOnLabel(rep_label, $"{currentRep}/{combo.reps}");
await Task.Run(() => ExecuteStrikes(combo.GetStrikes(), isAdvice, token));
if (!isAdvice) await Task.Delay(1200, token);
}
}
Код: Выделить всё
async public void Action()
{
cts = new CancellationTokenSource();
CancellationToken token = cts.Token;
int currentComIndex = 0;
int remainingRounds = workout.GetNumOfRounds();
int currentRound = 1;
double divisor = 1;
// Zobrazení času na pozadí
_ = Task.Run(() => ZobrazCas(token));
try
{
skipped = false;
await Task.Delay(2000, token);
await GenericSounds.PlayGetReadySound(token);
await Task.Delay(2000, token);
while (!end && !token.IsCancellationRequested)
{
// Pozastavení běhu, pokud je aktivní pauza
await CheckPauseAsync(token);
UpdateRoundAndComboLabels(currentRound, currentComIndex);
ComboClass combo = workout.GetCombos()[currentComIndex];
bool isAdvice = combo.GetStrikes()[0] is AdviceClass;
combo_label.Text = combo.GetName();
UpdateComboLabelFontSize(combo.GetName().Length);
UpdateStrikeLabelBackground(isAdvice);
await PlaySoundAsync(combo, token);
await Task.Delay(200, token);
// Start executing the combo
await ExecuteCombo(combo, isAdvice, token);
currentComIndex++;
if (currentComIndex == workout.TotalNumOfCombos())
{
currentComIndex = 0;
remainingRounds--;
currentRound++;
divisor *= 1.5;
}
if (remainingRounds == 0)
{
end = true;
}
skipped = false;
}
}
catch (TaskCanceledException)
{
// Log or handle the cancellation if needed
}
finally
{
cts.Dispose(); // Clean up the cancellation token source
}
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... n-maui-net
Мобильная версия