From d5684254a623a019773a44f42aa1045cdb477885 Mon Sep 17 00:00:00 2001 From: HiveBeats Date: Sat, 26 Oct 2024 11:57:45 +0700 Subject: [PATCH] IntialCommit --- .gitignore | 5 +++ ChannelsDispatcher.sln | 16 ++++++++++ ChannelsDispatcher/BackgroundInitializer.cs | 25 +++++++++++++++ ChannelsDispatcher/ChannelsDispatcher.csproj | 10 ++++++ ChannelsDispatcher/Program.cs | 22 ++++++++++++++ ChannelsDispatcher/ViewModel.cs | 32 ++++++++++++++++++++ 6 files changed, 110 insertions(+) create mode 100644 .gitignore create mode 100644 ChannelsDispatcher.sln create mode 100644 ChannelsDispatcher/BackgroundInitializer.cs create mode 100644 ChannelsDispatcher/ChannelsDispatcher.csproj create mode 100644 ChannelsDispatcher/Program.cs create mode 100644 ChannelsDispatcher/ViewModel.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..add57be --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +/packages/ +riderModule.iml +/_ReSharper.Caches/ \ No newline at end of file diff --git a/ChannelsDispatcher.sln b/ChannelsDispatcher.sln new file mode 100644 index 0000000..ca2cf8a --- /dev/null +++ b/ChannelsDispatcher.sln @@ -0,0 +1,16 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ChannelsDispatcher", "ChannelsDispatcher\ChannelsDispatcher.csproj", "{C251734B-3F10-477C-8775-F133D0C745D2}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C251734B-3F10-477C-8775-F133D0C745D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C251734B-3F10-477C-8775-F133D0C745D2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C251734B-3F10-477C-8775-F133D0C745D2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C251734B-3F10-477C-8775-F133D0C745D2}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/ChannelsDispatcher/BackgroundInitializer.cs b/ChannelsDispatcher/BackgroundInitializer.cs new file mode 100644 index 0000000..aa24870 --- /dev/null +++ b/ChannelsDispatcher/BackgroundInitializer.cs @@ -0,0 +1,25 @@ +using System.Threading.Channels; + +namespace ChannelsDispatcher; + +public class BackgroundInitializer +{ + private ChannelReader> _channel; + + public BackgroundInitializer(ChannelReader> channel) + { + _channel = channel; + } + + + public async Task ExecuteAsync() + { + while (true) + { + // получили задачу инциализации + var task = await _channel.ReadAsync(); + // исполнили задачу инициализации + await task.Invoke(); + } + } +} \ No newline at end of file diff --git a/ChannelsDispatcher/ChannelsDispatcher.csproj b/ChannelsDispatcher/ChannelsDispatcher.csproj new file mode 100644 index 0000000..2f4fc77 --- /dev/null +++ b/ChannelsDispatcher/ChannelsDispatcher.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/ChannelsDispatcher/Program.cs b/ChannelsDispatcher/Program.cs new file mode 100644 index 0000000..4525f3b --- /dev/null +++ b/ChannelsDispatcher/Program.cs @@ -0,0 +1,22 @@ +// See https://aka.ms/new-console-template for more information + +using System.Threading.Channels; +using ChannelsDispatcher; + +//AddSingleton +var channel = Channel.CreateUnbounded>(); +//AddSingleton +var channelReader = channel.Reader; +//AddSignleton +var channelWriter = channel.Writer; +//AddSingleton +var backgroundWorker = new BackgroundInitializer(channelReader); +Task.Run(async () => await backgroundWorker.ExecuteAsync()); +//AddScoped/AddTransient +var viewModel = new ViewModel(channelWriter); + +while (true) +{ + viewModel.DoUIStuff(); + await Task.Delay(TimeSpan.FromSeconds(1)); +} diff --git a/ChannelsDispatcher/ViewModel.cs b/ChannelsDispatcher/ViewModel.cs new file mode 100644 index 0000000..0273822 --- /dev/null +++ b/ChannelsDispatcher/ViewModel.cs @@ -0,0 +1,32 @@ +using System.Text; +using System.Threading.Channels; + +namespace ChannelsDispatcher; + +public class ViewModel +{ + private ChannelWriter> _channel; + public ViewModel(ChannelWriter> channel) + { + _channel = channel; + // чтобы не делать Write async, будем пытаться сделать это синхронно + bool success = false; + do + { + success = _channel.TryWrite(InitAsync); + } + while (!success); + } + + public async Task InitAsync() + { + Console.WriteLine("Initialization started. Will took 10 seconds"); + await Task.Delay(TimeSpan.FromSeconds(10)); + Console.WriteLine("ViewModel Initialized"); + } + + public void DoUIStuff() + { + Console.WriteLine("UI Not Freezed"); + } +} \ No newline at end of file