Skip to content

Instantly share code, notes, and snippets.

@noseratio
Created November 28, 2022 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noseratio/594d3722a65c6d7fd6e831e6131d433a to your computer and use it in GitHub Desktop.
Save noseratio/594d3722a65c6d7fd6e831e6131d433a to your computer and use it in GitHub Desktop.
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)
{
// UI thread
Debug.WriteLine(Thread.CurrentThread.IsThreadPoolThread);
await waitForEvent().ConfigureAwait(false);
// thread pool thread
Debug.WriteLine(Thread.CurrentThread.IsThreadPoolThread);
}
// client code
private async void Form1_Load(object sender, EventArgs e)
{
// UI thread
Debug.WriteLine(Thread.CurrentThread.IsThreadPoolThread);
await LibApiAsync(WaitForEvent);
// UI thread
Debug.WriteLine(Thread.CurrentThread.IsThreadPoolThread);
}
private Task WaitForEvent()
{
var tcs = new TaskCompletionSource();
void OnIdle(object? s, EventArgs e)
{
Application.Idle -= OnIdle;
tcs!.SetResult();
}
Application.Idle += OnIdle;
return tcs.Task;
}
public Form1()
{
InitializeComponent();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment