This file contains 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
#include <iostream> | |
#include <windows.h> | |
#include <processsnapshot.h> | |
int main() | |
{ | |
auto processHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, 40092); | |
if (processHandle == nullptr) |
This file contains 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
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net7.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
<PublishAot>true</PublishAot> | |
<IlcDisableReflection>true</IlcDisableReflection> | |
</PropertyGroup> |
This file contains 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
public static Task WhenAllOrFaulted(params Task[] tasks) | |
{ | |
var faultedContinuations = new Task[tasks.Length]; | |
var faultedTask = new TaskCompletionSource<Task>(); | |
for (int i = 0; i < tasks.Length; i++) | |
{ | |
faultedContinuations[i] = tasks[i].ContinueWith(t => faultedTask.SetResult(t), TaskContinuationOptions.OnlyOnFaulted); | |
} |
This file contains 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
public static Task WhenAllOrFaulted(params Task[] tasks) | |
{ | |
var faultedContinuations = new Task[tasks.Length]; | |
for (int i = 0; i < tasks.Length; i++) | |
{ | |
faultedContinuations[i] = tasks[i].ContinueWith(_ => {}, TaskContinuationOptions.OnlyOnFaulted); | |
} | |
var faultedTask = Task.WhenAny(faultedContinuations); |
This file contains 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
[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(); |
This file contains 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
[MethodImpl(MethodImplOptions.AggressiveOptimization)] | |
static async Task AsyncMethod() | |
{ | |
// Run a garbage collection in 1 second | |
Task.Delay(1000).ContinueWith(_ => { GC.Collect(); }); | |
var taskCompletionSource = new TaskCompletionSource(); | |
Action myDelegate = () => taskCompletionSource.SetResult(); |
This file contains 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
[MethodImpl(MethodImplOptions.AggressiveOptimization)] | |
static void Test() | |
{ | |
var obj = new object(); | |
// Run a garbage collection in 1 second | |
Task.Delay(1000).ContinueWith(_ => { GC.Collect(); }); | |
// As soon as MyNativeMethod is called, there are no more references to obj | |
MyNativeMethod(new WeakReference(obj)); |
This file contains 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
void Test() | |
{ | |
var obj = new Object(); | |
DoStuffWithObj(obj); | |
// At this point, the GC knows the object isn't used anymore and may collect obj | |
DoStuff(); | |
} |
This file contains 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
var taskCompletionSource = new TaskCompletionSource(); | |
MyDelegateType myDelegate = () => taskCompletionSource.SetResult(); | |
NativeMethods.MyNativeMethod(myDelegate); | |
await taskCompletionSource.Task; | |
GC.KeepAlive(myDelegate); |
This file contains 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
public unsafe class Getter | |
{ | |
private delegate*<Obj, SomeStruct*, SomeStruct*> _functionPointer; | |
public Getter(string propName) | |
{ | |
var methodInfo = typeof(Obj).GetProperty(propName).GetGetMethod(); | |
_functionPointer = (delegate*<Obj, SomeStruct*, SomeStruct*>)methodInfo.MethodHandle.GetFunctionPointer(); | |
} |
NewerOlder