using System; using System.Threading; namespace TestProject { class Program { static void Main(string[] args) { CreateInstanceOfA(); Console.ReadKey(); } static void CreateInstanceOfA() { var a = new A(); } } interface IA { void PrintHello(); } class A : IA { private B _b; public A() { _b = new B(this); } ~A() { Console.WriteLine("Instance of class A had been garbage-collected."); } public void PrintHello() { Console.WriteLine("Hello from class A!"); } } class B { private IA _a; private bool _working; public B(IA a) { _working = true; _a = a; var workingThread = new Thread(ThreadProc); workingThread.Start(); } ~B() { _working = false; Console.WriteLine("Instance of class B had been garbage-collected."); } private void ThreadProc() { while (_working) { _a.PrintHello(); Thread.Sleep(100); } } } }