• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DemoBytom / DemoEngine / 13296199563

12 Feb 2025 10:32PM UTC coverage: 10.129% (-0.01%) from 10.143%
13296199563

push

coveralls.net

DemoBytom
Moving main loop into a MainLoopService class

227 of 2241 relevant lines covered (10.13%)

22869.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Demo.Engine.Core/Services/EngineServiceNew.cs
1
// Copyright © Michał Dembski and contributors.
2
// Distributed under MIT license. See LICENSE file in the root for more information.
3

4
using System.Reflection;
5
using Demo.Engine.Core.Interfaces;
6
using Demo.Engine.Core.Interfaces.Platform;
7
using Demo.Engine.Core.Interfaces.Rendering;
8
using Microsoft.Extensions.DependencyInjection;
9
using Microsoft.Extensions.Hosting;
10
using Microsoft.Extensions.Logging;
11

12
namespace Demo.Engine.Core.Services;
13

14
internal sealed class EngineServiceNew(
×
15
    ILogger<EngineServiceNew> logger,
×
16
    IHostApplicationLifetime hostApplicationLifetime,
×
17
    IServiceScopeFactory scopeFactory,
×
18
    IMainLoopLifetime mainLoopLifetime)
×
19
    : IHostedService,
20
      IDisposable
21
{
22
    private readonly ILogger<EngineServiceNew> _logger = logger;
×
23
    private readonly IHostApplicationLifetime _hostApplicationLifetime = hostApplicationLifetime;
×
24
    private readonly IServiceScopeFactory _scopeFactory = scopeFactory;
×
25
    private readonly IMainLoopLifetime _mainLoopLifetime = mainLoopLifetime;
×
26
    private readonly string _serviceName = "Engine";
×
27
    private Task? _executingTask;
28
    private bool _stopRequested;
29
    private bool _disposedValue;
30

31
    private readonly string _version = Assembly
×
32
        .GetEntryAssembly()
×
33
        ?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
×
34
        ?.InformationalVersion ?? "0.0.0";
×
35

36
    public Task StartAsync(CancellationToken cancellationToken)
37
    {
38
        _logger.LogInformation("{serviceName} starting! v{version}", _serviceName, _version);
×
39
        _executingTask = DoWorkAsync();
×
40

41
        return _executingTask.IsCompleted
×
42
            ? _executingTask
×
43
            : Task.CompletedTask;
×
44
    }
45

46
    public async Task StopAsync(CancellationToken cancellationToken)
47
    {
48
        _logger.LogInformation("{serviceName} stopping!", _serviceName);
×
49

50
        _stopRequested = true;
×
51
        if (_executingTask is null)
×
52
        {
53
            return;
×
54
        }
55

56
        _ = await Task.WhenAny(
×
57
            _executingTask,
×
58
            Task.Delay(Timeout.Infinite, cancellationToken));
×
59
    }
×
60

61
    private async Task DoWorkAsync()
62
    {
63
        try
64
        {
65
            using var scope = _scopeFactory.CreateScope();
×
66
            var serviceProvider = scope.ServiceProvider;
×
67
            var osMessageHandler = serviceProvider.GetRequiredService<IOSMessageHandler>();
×
68
            var renderingEngine = serviceProvider.GetRequiredService<IRenderingEngine>();
×
69

70
            var mainLoopService = serviceProvider.GetRequiredService<IMainLoopServiceNew>();
×
71
            var executeAsync = mainLoopService.ExecutingTask;
×
72

73
            var staThreadService = serviceProvider.GetRequiredService<IStaThreadService>();
×
74
            var runStaThread = staThreadService.ExecutingTask;
×
75

76
            await Task.WhenAll(
×
77
                [
×
78
                    executeAsync,
×
79
                    runStaThread
×
80
                ]);
×
81
        }
×
82
        catch (OperationCanceledException)
×
83
        {
84
            _mainLoopLifetime.Cancel();
×
85
        }
×
86
        catch (Exception ex)
×
87
        {
88
            _logger.LogCritical(ex, "{serviceName} failed with error! {errorMessage}", _serviceName, ex.Message);
×
89
        }
×
90
        finally
91
        {
92
            if (!_stopRequested)
×
93
            {
94
                _hostApplicationLifetime.StopApplication();
×
95
            }
96
        }
97
    }
×
98

99
    private void Dispose(bool disposing)
100
    {
101
        if (!_disposedValue)
×
102
        {
103
            if (disposing)
×
104
            {
105
                _hostApplicationLifetime.StopApplication();
×
106
            }
107

108
            _disposedValue = true;
×
109
        }
110
    }
×
111

112
    public void Dispose()
113
    {
114
        // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
115
        Dispose(disposing: true);
×
116
        GC.SuppressFinalize(this);
×
117
    }
×
118
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc