Skip to content

Instantly share code, notes, and snippets.

View noseratio's full-sized avatar

Andrew Nosenko noseratio

View GitHub Profile
@noseratio
noseratio / SwitchTo.cs
Last active May 2, 2023 11:17
TaskScheduler.Default.SwitchTo
// md wfapp
// dotnet new winforms
// then copy/paste this
using System.Diagnostics;
namespace wfapp;
public partial class Form1 : Form
{
@noseratio
noseratio / wfapp.cs
Created November 28, 2022 10:00
CF pushes us to ThreadPool
// https://twitter.com/noseratio/status/1597159958734217217
using System.Diagnostics;
namespace wfapp;
public partial class Form1 : Form
{
// library code
private static async Task LibApiAsync(Func<Task> waitForEvent)
@noseratio
noseratio / DebugSyncContext.cs
Last active November 9, 2023 19:16
A simple helper SynchronizationContext to debug deadlocks
// A helper SynchronizationContext to debug deadlocks by @noseratio
// If you install DebugSyncContext at the very beginning of your console app and run it under debugger,
// it should stop where the deadlock is happening, with a better access to the stack frame.
using System.Diagnostics;
SynchronizationContext.SetSynchronizationContext(new DebugSyncContext());
Console.WriteLine("Hello!");
@noseratio
noseratio / AsyncQueue.cs
Last active November 9, 2023 19:18
Simple async wrapper around .NET Queue
public sealed class AsyncQueue<T>: IAsyncDisposable
{
private readonly Queue<T> _queue = new();
private readonly SemaphoreSlim _semaphore = new(initialCount: 1, maxCount: 1);
private readonly CancellationTokenSource _cts = new();
private TaskCompletionSource _itemTcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
public async ValueTask<(bool, T)> TryPeekAsync(CancellationToken cancelToken)
{
await _semaphore.WaitAsync(cancelToken);
@noseratio
noseratio / GenHostConApp.cs
Last active May 2, 2023 11:18
A minimal console app using .NET Generic Host API
// https://twitter.com/noseratio/status/1535173151910350848
// https://docs.microsoft.com/en-us/dotnet/core/extensions/generic-host
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using var host = Host.CreateDefaultBuilder(Environment.GetCommandLineArgs())
.ConfigureLogging(
logging =>
@noseratio
noseratio / LazyVsNoLazy.cs
Created June 9, 2022 23:41
Lazy vs no Lazy for IObservable pipeline construction
// by Theodor Zoulias
// https://stackoverflow.com/questions/72558093/in-rx-net-how-do-i-make-a-subject-to-resemble-taskcompletionsource-behavior/72561100?noredirect=1#comment128188846_72561100
//
// comment out "#define USING_LAZY"
#define USING_LAZY
using System;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Threading.Tasks;
@noseratio
noseratio / RxWithCancellation.cs
Last active February 14, 2024 00:17
Cancelling an observable with a CancellationToken
// https://stackoverflow.com/q/72471152/1768303
// https://dotnetfiddle.net/bLYDgw
/*
I don't think I should be seeing "Emitting: 19" after "Cancelling"
Emitting: 18
TakeUntil passed: 18
OnNext: 18
Cancelling
@noseratio
noseratio / rx-net-resources.md
Last active May 2, 2023 11:19
Rx .NET Resources
@noseratio
noseratio / AsyncQueue.cs
Created May 16, 2022 21:58
An async queue which can be cleared from a producer end
// https://twitter.com/noseratio/status/1526314613364490241
public sealed class AsyncQueue<T>: IAsyncDisposable
{
private readonly Queue<T> _queue = new();
private readonly SemaphoreSlim _semaphore = new(initialCount: 1);
private readonly CancellationTokenSource _cts = new();
private TaskCompletionSource<DBNull> _itemTcs = new(TaskCreationOptions.RunContinuationsAsynchronously);
public async Task Clear(CancellationToken cancelToken)
@noseratio
noseratio / reset-twitter-interest.md
Last active March 5, 2024 07:26
Reset Twitter interests