IntialCommit
This commit is contained in:
25
ChannelsDispatcher/BackgroundInitializer.cs
Normal file
25
ChannelsDispatcher/BackgroundInitializer.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
10
ChannelsDispatcher/ChannelsDispatcher.csproj
Normal file
10
ChannelsDispatcher/ChannelsDispatcher.csproj
Normal 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>
|
||||
22
ChannelsDispatcher/Program.cs
Normal file
22
ChannelsDispatcher/Program.cs
Normal 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));
|
||||
}
|
||||
32
ChannelsDispatcher/ViewModel.cs
Normal file
32
ChannelsDispatcher/ViewModel.cs
Normal 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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user