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

DemoBytom / DemoEngine / 24940020806

25 Apr 2026 08:30PM UTC coverage: 28.251% (-1.5%) from 29.75%
24940020806

push

coveralls.net

DemoBytom
Remove old `StaThreadService` and related codebase

Superseeded by `WindowsMessagePump`

1208 of 4276 relevant lines covered (28.25%)

0.32 hits per line

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

0.0
/src/Demo.Engine.Core/Services/EngineService.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.Requests;
7
using Mediator;
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 EngineService(
×
15
    ILogger<EngineService> logger,
×
16
    IHostApplicationLifetime hostApplicationLifetime,
×
17
    IServiceScopeFactory scopeFactory,
×
18
    IMediator mediator)
×
19
    : IHostedService,
20
      IDisposable
21
{
22
    private readonly ILogger<EngineService> _logger = logger;
×
23
    private readonly IHostApplicationLifetime _hostApplicationLifetime = hostApplicationLifetime;
×
24
    private readonly IServiceScopeFactory _scopeFactory = scopeFactory;
×
25
    private readonly IMediator _mediator = mediator;
×
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.LogServiceIsStarting(_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.LogServiceStopping(_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
        await using var scope = _scopeFactory.CreateAsyncScope();
×
64
        var serviceProvider = scope.ServiceProvider;
×
65
        var mainLoopLifetime = serviceProvider.GetService<IMainLoopLifetime>();
×
66

67
        try
68
        {
69
            _ = await _mediator.Send(new CompileShaders(), _hostApplicationLifetime.ApplicationStopping);
×
70
            _ = await _mediator.Send(new LoadShadersRequest(), _hostApplicationLifetime.ApplicationStopping);
×
71

72
            var mainLoopService = serviceProvider.GetRequiredService<IMainLoopService>();
×
73

74
            await mainLoopService.ExecutingTask;
×
75
        }
×
76
        catch (OperationCanceledException)
×
77
        {
78
            mainLoopLifetime?.Cancel();
×
79
        }
×
80
        catch (Exception ex)
×
81
        {
82
            _logger.LogServiceFailedWithError(ex, _serviceName);
×
83
        }
×
84
        finally
85
        {
86
            if (!_stopRequested)
×
87
            {
88
                await scope.DisposeAsync();
×
89
                _hostApplicationLifetime.StopApplication();
×
90
            }
91
        }
92
    }
×
93

94
    private void Dispose(bool disposing)
95
    {
96
        if (!_disposedValue)
×
97
        {
98
            if (disposing)
×
99
            {
100
                _hostApplicationLifetime.StopApplication();
×
101
            }
102

103
            _disposedValue = true;
×
104
        }
105
    }
×
106

107
    public void Dispose()
108
    {
109
        // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
110
        Dispose(disposing: true);
×
111
        GC.SuppressFinalize(this);
×
112
    }
×
113
}
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