{"id":9524,"date":"2024-10-31T19:10:02","date_gmt":"2024-10-31T10:10:02","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=9524"},"modified":"2024-10-31T19:10:02","modified_gmt":"2024-10-31T10:10:02","slug":"c-prism-backgroundservice-%ec%83%9d%ec%84%b1%ed%95%98%ea%b8%b0","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=9524","title":{"rendered":"C# Prism &#8211; BackgroundService \uc0dd\uc131\ud558\uae30"},"content":{"rendered":"<h1>C# Prism &#8211; BackgroundService \uc0dd\uc131\ud558\uae30<\/h1>\n<p>\ub180\ub78d\uac8c\ub3c4 \uc778\uacf5\uc9c0\ub2a5\uc774 \ucf54\ub529\ud55c \ucf54\ub4dc\uc785\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-csharp\">public interface IBackgroundService\n{\n    Task StartAsync();\n    Task StopAsync();\n}\n\npublic class BackgroundService : IBackgroundService\n{\n    private readonly CancellationTokenSource _cts = new CancellationTokenSource();\n    private readonly IEventAggregator _eventAggregator;\n\n    public BackgroundService(IEventAggregator eventAggregator)\n    {\n        _eventAggregator = eventAggregator;\n    }\n\n    public async Task StartAsync()\n    {\n        await Task.Run(async () =&gt;\n        {\n            try\n            {\n                while (!_cts.Token.IsCancellationRequested)\n                {\n                    await DoWorkAsync();\n                    \/\/ \ud544\uc694\ud55c \uacbd\uc6b0 \uc774\ubca4\ud2b8 \ubc1c\uc0dd\n                    await _eventAggregator.PublishAsync(new WorkCompletedEvent());\n                    await Task.Delay(1000, _cts.Token);\n                }\n            }\n            catch (OperationCanceledException)\n            {\n                \/\/ \uc815\uc0c1\uc801\uc778 \uc885\ub8cc\n            }\n        }, _cts.Token);\n    }\n\n    public Task StopAsync()\n    {\n        _cts.Cancel();\n        return Task.CompletedTask;\n    }\n\n    private async Task DoWorkAsync()\n    {\n        \/\/ \uc2e4\uc81c \uc791\uc5c5 \uc218\ud589\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-csharp\">public partial class App : PrismApplication\n{\n    private IBackgroundService _backgroundService;\n\n    protected override void OnInitialized()\n    {\n        base.OnInitialized();\n\n        \/\/ DI \ucee8\ud14c\uc774\ub108\uc5d0\uc11c \uc11c\ube44\uc2a4 \uac00\uc838\uc624\uae30\n        _backgroundService = Container.Resolve&lt;IBackgroundService&gt;();\n        \/\/ \uc11c\ube44\uc2a4 \uc2dc\uc791\n        _backgroundService.StartAsync();\n    }\n\n    protected override void OnExit(ExitEventArgs e)\n    {\n        \/\/ \uc11c\ube44\uc2a4 \uc885\ub8cc\n        _backgroundService?.StopAsync().Wait();\n        base.OnExit(e);\n    }\n\n    protected override void RegisterTypes(IContainerRegistry containerRegistry)\n    {\n        containerRegistry.RegisterSingleton&lt;IBackgroundService, BackgroundService&gt;();\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>C# Prism &#8211; BackgroundService \uc0dd\uc131\ud558\uae30 \ub180\ub78d\uac8c\ub3c4 \uc778\uacf5\uc9c0\ub2a5\uc774 \ucf54\ub529\ud55c \ucf54\ub4dc\uc785\ub2c8\ub2e4. public interface IBackgroundService { Task StartAsync(); Task StopAsync(); } public class BackgroundService : IBackgroundService { private readonly CancellationTokenSource _cts = new CancellationTokenSource(); private readonly IEventAggregator _eventAggregator; public BackgroundService(IEventAggregator eventAggregator) { _eventAggregator = eventAggregator; } public async Task StartAsync() { await Task.Run(async () =&gt; { try {\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=9524\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-9524","post","type-post","status-publish","format-standard","hentry","category-language"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9524","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9524"}],"version-history":[{"count":1,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9524\/revisions"}],"predecessor-version":[{"id":9525,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/9524\/revisions\/9525"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9524"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9524"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9524"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}