Created
December 14, 2020 22:26
-
-
Save Quenty/5e752cead6644a3faa14f45647d15b46 to your computer and use it in GitHub Desktop.
C# verification that events GC when connected with static functions. This snippet proves that this is the case.
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
----- BUILD EVENT CONNECTED ----- | |
[HandleEventStatic] invoked with message Firing while we still have a reference | |
----- CHECK REFERENCE ----- | |
[CheckReference] - ERROR: Object was not GCed | |
[HandleEventStatic] invoked with message Hello from check reference | |
----- FORCE GARBAGE COLLECTION ----- | |
ObjectWithEvent deconstructor called | |
----- CHECK REFERENCE ----- | |
[CheckReference] - SUCCESS: Object was GCed |
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
using System; | |
namespace TestEventBehaviorConsoleApp | |
{ | |
class MessageEventArgs : EventArgs | |
{ | |
public string Message { get; set; } | |
public MessageEventArgs(string message) | |
{ | |
this.Message = message; | |
} | |
} | |
class ObjectWithEvent | |
{ | |
public event EventHandler<MessageEventArgs> Event; | |
public void FireEvent(string message) | |
{ | |
Event?.Invoke(this, new MessageEventArgs(message)); | |
} | |
~ObjectWithEvent() | |
{ | |
Console.WriteLine("ObjectWithEvent deconstructor called"); | |
} | |
} | |
class Program | |
{ | |
static WeakReference<ObjectWithEvent> BuildWeakReference() | |
{ | |
ObjectWithEvent obj = new ObjectWithEvent(); | |
obj.Event += HandleEventStatic; | |
WeakReference<ObjectWithEvent> reference = new WeakReference<ObjectWithEvent>(obj); | |
obj.FireEvent("Firing while we still have a reference"); | |
return reference; | |
} | |
static void CheckReference(WeakReference<ObjectWithEvent> reference) | |
{ | |
if (reference.TryGetTarget(out ObjectWithEvent outputEvent)) | |
{ | |
Console.WriteLine("[CheckReference] - ERROR: Object was not GCed"); | |
outputEvent.FireEvent("Hello from check reference"); // Use this incase the compiler is sane | |
} | |
else | |
{ | |
Console.WriteLine("[CheckReference] - SUCCESS: Object was GCed"); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
Console.WriteLine("----- BUILD EVENT CONNECTED -----"); | |
Console.WriteLine(); | |
WeakReference<ObjectWithEvent> reference = BuildWeakReference(); | |
Console.WriteLine(); | |
Console.WriteLine("----- CHECK REFERENCE -----"); | |
Console.WriteLine(); | |
CheckReference(reference); | |
Console.WriteLine(); | |
Console.WriteLine("----- FORCE GARBAGE COLLECTION -----"); | |
Console.WriteLine(); | |
// Force GC | |
GC.Collect(); | |
GC.WaitForPendingFinalizers(); | |
Console.WriteLine(); | |
Console.WriteLine("----- CHECK REFERENCE -----"); | |
Console.WriteLine(); | |
CheckReference(reference); | |
// Delay | |
Console.ReadKey(); | |
} | |
private static void HandleEventStatic(object sender, MessageEventArgs args) | |
{ | |
Console.WriteLine($"[HandleEventStatic] invoked with message {args.Message}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment