IntialCommit

This commit is contained in:
2024-10-26 11:57:45 +07:00
commit d5684254a6
6 changed files with 110 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
bin/
obj/
/packages/
riderModule.iml
/_ReSharper.Caches/

16
ChannelsDispatcher.sln Normal file
View File

@@ -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

View File

@@ -0,0 +1,25 @@
using System.Threading.Channels;
namespace ChannelsDispatcher;
public class BackgroundInitializer
{
private ChannelReader<Func<Task>> _channel;
public BackgroundInitializer(ChannelReader<Func<Task>> channel)
{
_channel = channel;
}
public async Task ExecuteAsync()
{
while (true)
{
// получили задачу инциализации
var task = await _channel.ReadAsync();
// исполнили задачу инициализации
await task.Invoke();
}
}
}

View File

@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -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<Func<Task>>();
//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));
}

View File

@@ -0,0 +1,32 @@
using System.Text;
using System.Threading.Channels;
namespace ChannelsDispatcher;
public class ViewModel
{
private ChannelWriter<Func<Task>> _channel;
public ViewModel(ChannelWriter<Func<Task>> 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");
}
}