Skip to content

Instantly share code, notes, and snippets.

@kevingosse
Created September 22, 2022 11:05
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 kevingosse/807178ed95409b82613089ec2c795ab5 to your computer and use it in GitHub Desktop.
Save kevingosse/807178ed95409b82613089ec2c795ab5 to your computer and use it in GitHub Desktop.
[MethodImpl(MethodImplOptions.AggressiveOptimization)]
static void SyncMethod()
{
// Run a garbage collection in 1 second
Task.Delay(1000).ContinueWith(_ => { GC.Collect(); });
var taskCompletionSource = new TaskCompletionSource();
Action myDelegate = () => taskCompletionSource.SetResult();
MyNativeMethod(new WeakReference(myDelegate));
taskCompletionSource.Task.Wait(); // Wait synchronously on the TCS
GC.KeepAlive(myDelegate);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment