вторник, 28 ноября 2023 г.

Task, Delay

https://learn.microsoft.com/ru-ru/dotnet/api/system.threading.tasks.task.delay?view=net-8.0

using System.Runtime.InteropServices;

using System;

using System.Diagnostics;

namespace CaWorker01;


Task.Delay(1000, CancellationToken.None)

public class DelayExamples

{

    public async void tLoadPads()

    {

        while (bIsEnumeratingSongs)

        {

            await Task.Delay(100);

        }

        CSongDict.tRefreshScoreTables();

    }

    private void ScheduleCheck()

    {

        System.Threading.Tasks.Task.Delay(500).ContinueWith(t =>

        {

            if (Test())

                return;

            ScheduleCheck();

        });

    }

    public async Task Stop()

    {

        _source.Cancel();

        _source = null;

        while (!_stopped)

        {

            await Task.Delay(100).ConfigureAwait(false);

        }

    }

    public static async Task Log()

    {

        while (running)

        {

            await Task.Delay(1000);

            Console.WriteLine(operations);

            operations = 0;

        }

    }

    internal static void GcTask()

    {

        if (NeedGc)

        {

            GC.Collect();

            Task.Delay(1000).Wait();

            GC.Collect();

            NeedGc = false;

        }

    }

    static async void DoWithDelay(int millis, Action<int> action)

    {

        int i = 0;

        while (true)

        {

            await Task.Delay(millis);

            action(i);

            i++;

        }

    }

    [HttpPost]

    [Route("random-number")]

    public async Task<int> RandomNumber()

    {

        await Task.Delay(2000);

        lock (random)

        {

            return random.Next(100);

        }

    }

    public static async Task WaitIPV6Connect()

    {

        while (true)

        {

            if (await Ipv6Connected())

                break;


            await Task.Delay(500);

        }

    }

    private async Task<int> OnPlayerDisconnectAsync(IPlayer player, string reason)

    {

        if (player is IMyPlayer unused3)

        {  }

        await Task.Delay(1000);

        return 42;

    }

    public static async Task<int> GetIntegerSumAsync1(IEnumerable<int> items)

    {

        await Task.Delay(100);

        int num = 0;

        foreach (int item in items)

        {

            num += item;

        }

        return num;

    }

    public static async ValueTask<int> GetIntegerSumAsync2(IEnumerable<int> items)

    {

        await Task.Delay(100);

        int num = 0;

        foreach (int item in items)

        {

            num += item;

        }

        return num;

    }

    public async Task<int> LongOp(int n, IProgress<int> progress)

    {

        for (int i = 0; i < n; i++)

        {

            progress.Report(i);

            await Task.Delay(100);

        }

        return n;

    }

    private async void EnsureWindowIsForeground()

    {

        for (var i = 0; i < 100; i++)

        {

            await Task.Delay(10);


            if (_mainWindow == null) return;

            if (HideWindowIfNotForeground()) return;

        }

    }

    private async Task ReCheck()

    {

        if (_disposedValue)

        {

            return;

        }

        _currentRoom.Monitor.Check(TriggerType.HttpApiRecheck);

        await Task.Delay((int)_currentRoom.TimingStreamRetry + 1000);

    }

    public UnconspicousWindow()

    {

        InitializeComponent();


        this.Loaded += async (sender, args) =>

        {

            await Task.Delay(5000);

            this.Dispatcher.Invoke(this.Close);

        };

    }

    private async void FadeIn()

    {

        //Fades in

        for (double i = 0; i <= 1; i += 0.1)

        {

            this.Opacity = i;

            mainBorder.Opacity = i;

            await Task.Delay(waitDelay);

        }

    }

    private static async Task<Disposable> CreateAsync(bool b)

    {

        if (b)

        {

            throw new InvalidOperationException("Expected");

        }

        await Task.Delay(1);

        return new Disposable();

    }

    public async Task<bool> WaitForAllTasksFinished()

    {

        var didWait = false;

        while (!AreAllAsyncTasksDone)

        {

            didWait = true;

            await Task.Delay(200);

        }

        return didWait;

    }

    private static async void OnAgentServerDisconnected()

    {

        if (!Game.Clientless)

            return;

        Log.Warn("Attempting relogin in 10 seconds...");

        await Task.Delay(10000);

        Game.Start();

    }

    public void RestartScript()

    {

        Trace.WriteLine("Restarting script");

        StopScript(false);

        Task.Run(async () =>

        {

            await Task.Delay(5000);

            await StartScriptAsync();

        });

    }

    public static async Task<SimpleValue> Healthy(string id)

    {

        await Task.Delay(10).ConfigureAwait(false);

        return new SimpleValue

        {

            Id = id,

            Value = $"The key is {id}"

        };

    }

    public async Task<string> AsyncStringDelay(string s, int ms)

    {

#if NET_45

            await Task.Delay(ms);

#elif NET_40

            await TaskEx.Delay(ms);

#endif

        return s;

    }

    private async void LoadSamples()

    {

        this.DataContext = await Sample.LoadFromCurrentAssemblyAsync();

        await Task.Delay(50);

        this.ListBox.SelectedItem = null;

        this.TopFlyout.IsOpen = true;

    }

    private async void SubscribeToWindowState()

    {

        var hostWindow = (Window?)VisualRoot;

        while (hostWindow == null)

        {

            hostWindow = (Window?)VisualRoot;

            await Task.Delay(50);

        }

    }

    private async void DynamicUpdate()

    {

        double badgeText = 1;

        while (true)

        {

            badgeText += 1;

            this.chatBadge.BadgeText = badgeText.ToString();

            await Task.Delay(2000);

        }

    }

}

четверг, 2 ноября 2023 г.

System.Threading, System,Threading, Async, Task, TaskCompletionSource

Threads
https://learn.microsoft.com/pdf?url=https%3A%2F%2Flearn.microsoft.com%2Fru-ru%2Fdotnet%2Fnavigate%2Fadvanced-programming%2Ftoc.json

ASync

System.Threading.Task

Task.Delay

TaskCompletionSource


Starvation, Bombardier, Counts, Stack

Starvation

https://learn.microsoft.com/ru-ru/dotnet/core/diagnostics/debug-threadpool-starvation

Bombardier

https://github.com/codesenberg/bombardier/releases

Counts

https://learn.microsoft.com/ru-ru/dotnet/core/diagnostics/dotnet-counters

Stack

https://learn.microsoft.com/ru-ru/dotnet/core/diagnostics/dotnet-stack

Async, Sync, ThreadPool, Stephen Toub

Async, Sync, ThreadPool

https://devblogs.microsoft.com/pfxteam/should-i-expose-synchronous-wrappers-for-asynchronous-methods/

https://devblogs.microsoft.com/pfxteam/tag/threadpool/

https://devblogs.microsoft.com/pfxteam/author/toub/

TreadPool, Articles

TreadPool, Articles

https://learn.microsoft.com/ru-ru/search/?terms=ThreadPool

ThreadPool, Min, Max

https://learn.microsoft.com/ru-ru/dotnet/api/system.threading.threadpool.setminthreads?view=net-7.0

https://learn.microsoft.com/ru-ru/dotnet/api/system.threading.threadpool.setmaxthreads?view=net-7.0

https://learn.microsoft.com/ru-ru/dotnet/core/runtime-config/threading

среда, 1 ноября 2023 г.

ThreadPool, QueueUserWorkItem, AutoResetEvent, ManualResetEvent, EventWaitHandle

The managed thread pool



ThreadPool.QueueUserWorkItem

https://learn.microsoft.com/ru-ru/dotnet/api/system.threading.threadpool.queueuserworkitem?view=net-7.0

ManualResetEvent Класс

https://learn.microsoft.com/ru-ru/dotnet/api/system.threading.manualresetevent?view=net-7.0

AutoResetEvent

https://learn.microsoft.com/ru-ru/dotnet/api/system.threading.autoresetevent?view=net-7.0

EventWaitHandle

Async, Pdf

https://learn.microsoft.com/ru-ru/dotnet/standard/threading/the-managed-thread-pool

https://learn.microsoft.com/en-us/dotnet/standard/threading/the-managed-thread-pool

https://learn.microsoft.com/pdf?url=https%3A%2F%2Flearn.microsoft.com%2Fru-ru%2Fdotnet%2Fnavigate%2Fadvanced-programming%2Ftoc.json

Асинхронное программирование

Потоки

Параллельное программирование

Взаимодействие на уровне машинного кода

Управление памятью