Skip to content

Instantly share code, notes, and snippets.

View noseratio's full-sized avatar

Andrew Nosenko noseratio

View GitHub Profile
@noseratio
noseratio / GenerateWithoutYield.cs
Last active May 2, 2023 11:21
Re-implement an `IEnumerable` method without `yield`
// https://twitter.com/noseratio/status/1491189525061980160
using System.Collections;
using System.Diagnostics.CodeAnalysis;
foreach (var item in Generate())
{
Console.WriteLine(item);
}
@noseratio
noseratio / WinFormsAsyncEnumerable.cs
Created February 7, 2022 05:02
Streaming TextChanged event as IAsyncEnumerable
// verifying https://twitter.com/noseratio/status/1490181545746841602?s=20&t=9FIQA5E2Qz9oHLSEQIW7Ew
namespace textbox;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Channels;
static class Program
{
@noseratio
noseratio / EventLoopThreadScheduler.cs
Last active February 2, 2022 03:15
Single thread event loop scheduler, based on Rx EventLoopScheduler but with added SynchronizationContext and TPL TaskScheduler API
namespace Noseratio;
/// <summary>
/// Single thread event loop scheduler, based on Rx <seealso cref="EventLoopScheduler"/>
/// but with added SynchronizationContext and TPL TaskScheduler API
/// https://gist.github.com/noseratio/22a291e1d69f6d1cea547623ad9c9147
/// </summary>
public sealed partial class EventLoopThreadScheduler :
TaskScheduler,
IScheduler,
@noseratio
noseratio / LongRunningAsyncDisposable.cs
Last active January 20, 2022 11:21
LongRunningAsyncDisposable implements a long-running task that can be stopped by calling IAsyncDisposable.DisposeAsync in a thread-safe, concurrency-friendly way
// https://stackoverflow.com/q/70718409/1768303
try
{
await using var service = new BackgroundService(
duration: TimeSpan.FromSeconds(2),
catchRuntimeErrorsAtDispose: true);
await Task.WhenAll(
Enumerable.Range(0, 10).Select(i =>
@noseratio
noseratio / Form1.cs
Created December 2, 2021 10:22
async void event handlers and reentrancy
// https://twitter.com/noseratio/status/1466350790952456192?s=20
// dotnet new winforms -n app
// paste this into Form1.cs
namespace app;
public partial class Form1 : Form
{
int _count = 0;
Button _button;
@noseratio
noseratio / ConsoleHost.cs
Created November 17, 2021 08:45
Generic .NET console app with DI and life cycle
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace ConsoleDisposable.Example
{
public class TransientDisposable : IDisposable
{
@noseratio
noseratio / WinForms-wish-list.md
Last active November 16, 2021 01:58
Wish list for WinForms in .NET 7.0
@noseratio
noseratio / remove-unwanted-windows11-apps.ps1
Last active April 13, 2024 04:57
Windows 11 autorun resident apps that I have to remove manually
# Teams 2.0 (no work account support yet)
winget uninstall MicrosoftTeams_8wekyb3d8bbwe
# Your Phone
winget uninstall Microsoft.YourPhone_8wekyb3d8bbwe
# Widgets (Web Experience Pack)
winget uninstall MicrosoftWindows.Client.WebExperience_cw5n1h2txyewy
# Cortana
@noseratio
noseratio / Diagnostics.cs
Created October 19, 2021 06:36
Double-click the output in Visual Studio Output Window to go to that source file and line
internal static partial class Diagnostics
{
/// <summary>
/// Double-click the output in Visual Studio Output Window to go to that source file and line
/// </summary>
[Conditional("DEBUG")]
public static void Log(
string message,
[CallerMemberName] string callerName = "",
[CallerLineNumber] int lineNumber = 0,
@noseratio
noseratio / WinFormsApp.cs
Created September 27, 2021 10:50
Monitor.Enter pumps some messages including WM_PAINT
// https://github.com/mono/SkiaSharp/issues/1383
// based on https://gist.github.com/retran/b57e4db1a173048c2cee49ac6d523fc2
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WutExample