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(int i); } 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(int i) { Console.WriteLine("Hello from class A! Current iterations count: {0}", i); } } 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() { const int MAX_ITERATIONS_COUNT = 100; int i = 0; while (_working && i++ < MAX_ITERATIONS_COUNT) { _a.PrintHello(i); Thread.Sleep(100); } } } }