Created
November 28, 2022 10:00
-
-
Save noseratio/594d3722a65c6d7fd6e831e6131d433a to your computer and use it in GitHub Desktop.
CF pushes us to ThreadPool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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