0
\$\begingroup\$

first of all im not using unity. it's a Signalr .Net Backend Program that runs turn-based multiplayer games.

I use c# async methods to make multiplayer scenarios. for example, the player has 10 seconds to finish his turn. after that the backend side wraps the round.

public async Task RunPlayerTurn(){
// send player his turn started
   await Task.Delay(10000);
// send player his turn has finished
}

the system works fine and has no problem until I want to handle the player disconnection scenario. assume one side disconnects and i want to stop the timer and simulation and call another player that should wait.

basically, there is no generalized way to pause any async method in c#. i should implement my own logic that is not clean.

I'm not sure but maybe there is a timer approach or library that I can assign a list of tasks on different times. for example, telling the timer that calls this method in the next 10 seconds or another one in 20 seconds and by just pausing that timer all simulation can be freezed.

i know there are schedulers like hangfire or ... that can be used. but it's said that they used databases and are not performant for short-lived lots of tasks.

\$\endgroup\$
9
  • \$\begingroup\$ Have you considered using the same trick Unity uses with IEnumerator methods returning YieldInstructions? \$\endgroup\$
    – DMGregory
    Aug 4 at 14:03
  • \$\begingroup\$ @DMGregory i was considering that but i think that is old solution as unity did not support async for long time. at the moment im looking for some schedulers like fluentScheduler. it seems ok but performance and side effects should be tested. \$\endgroup\$
    – virtouso
    Aug 4 at 14:09
  • \$\begingroup\$ I'm not very fluent in C# threading, so I might not be understanding the situation completely, but is this the sort of thing you're after: C# pattern to pause/resume an async task? \$\endgroup\$
    – Pikalek
    Aug 4 at 14:59
  • \$\begingroup\$ @DMGregory i think unity coroutines use frame counter internally so i dont think they can be used outside of unity unless i implement something similar with ienumerables. \$\endgroup\$
    – virtouso
    Aug 4 at 15:24
  • 2
    \$\begingroup\$ And async tasks can be dangerous, because they use multithreading. That means you need to make sure that everything you schedule is thread-safe. There are really just two situations where I would recommend multithreading: Parallelizing expensive computations to make use of multiple CPU cores and staying responsive while running them. All other situations are usually not worth the risk. An own timed event queue which gets checked at a known point of your main game loop is far more predictable. \$\endgroup\$
    – Philipp
    Aug 4 at 16:15

0

You must log in to answer this question.

Browse other questions tagged .