patterncsharpMinor
Achieving a never-ending loop of game tasks
Viewed 0 times
neverachievingloopgametasksending
Problem
The following is a basic implementation of a loop which is intended to launch a series of games in a loop (generic ones that can be overridden, but that have always have finite durations). The intention would be that a game can broadcast events to clients and would always broadcast its completion. When there are no clients listening it can
Is it a good way to approach this? Does it matter that I do not await the
```
public class Table
{
private readonly GameScheduler _gameScheduler;
public Game Game { get; set; }
public Table()
{
_gameScheduler = new GameScheduler();
Task schedulerTask = _gameScheduler.Start(this);
}
public void Close()
{
_gameScheduler.Shutdown();
}
}
public class GameScheduler
{
private bool _running = true;
public async Task Start(Table t)
{
while (_running)
{
await RunGame(t);
}
}
async Task RunGame(Table table)
{
var game = new Game();
table.Game = game;
table.Game.Start();
await table.Game.GameCompletionSource.Task;
}
public void Shutdown() { _running = false; }
}
public class Game
{
System.Threading.Timer _countdownTimer;
private int _timeLeft = 15000;
private int _countdownInterval = 1000;
public TaskCompletionSource GameCompletionSource;
public void Start()
{
_countdownTimer = new System.Threading.Timer(GameCountdown, null, 0, _countdownInterval);
}
public void GameCountdown(object state)
{
if (_timeLeft <= 0)
{
_countdownTimer.Dispose();
Finish();
}
else
{
_timeLeft -= _countdownInterval;
}
}
private void Finish()
{
GameCompletionSource.SetResult(true);
}
Shutdown, and restart again when some clients begin listening againIs it a good way to approach this? Does it matter that I do not await the
_gameScheduler.Start(this) call? Am I dealing with the task loop correctly? Is the timer used correctly?```
public class Table
{
private readonly GameScheduler _gameScheduler;
public Game Game { get; set; }
public Table()
{
_gameScheduler = new GameScheduler();
Task schedulerTask = _gameScheduler.Start(this);
}
public void Close()
{
_gameScheduler.Shutdown();
}
}
public class GameScheduler
{
private bool _running = true;
public async Task Start(Table t)
{
while (_running)
{
await RunGame(t);
}
}
async Task RunGame(Table table)
{
var game = new Game();
table.Game = game;
table.Game.Start();
await table.Game.GameCompletionSource.Task;
}
public void Shutdown() { _running = false; }
}
public class Game
{
System.Threading.Timer _countdownTimer;
private int _timeLeft = 15000;
private int _countdownInterval = 1000;
public TaskCompletionSource GameCompletionSource;
public void Start()
{
_countdownTimer = new System.Threading.Timer(GameCountdown, null, 0, _countdownInterval);
}
public void GameCountdown(object state)
{
if (_timeLeft <= 0)
{
_countdownTimer.Dispose();
Finish();
}
else
{
_timeLeft -= _countdownInterval;
}
}
private void Finish()
{
GameCompletionSource.SetResult(true);
}
Solution
I just want to say that I review C# code style a lot on this site, and this code made me very happy for many seconds, until I spotted this:
So close!
I don't like underscore notation personally as it smells strongly Hungarian, but it's a commonly-accepted system in C# so I don't have a leg to stand on.
public async Task Start(Table t)So close!
t should, of course, have a much more descriptive name here. You've even done that elsewhere, so it is most likely an oversight on your part.I don't like underscore notation personally as it smells strongly Hungarian, but it's a commonly-accepted system in C# so I don't have a leg to stand on.
Code Snippets
public async Task Start(Table t)Context
StackExchange Code Review Q#74537, answer score: 2
Revisions (0)
No revisions yet.