public void CallMeFromMainThread() { // create an instance ThreadsShareInstanceVaraibles tt = new ThreadsShareInstanceVaraibles(); Task t = new Task(tt.ThreadSafeGo); t.Start(); tt.ThreadSafeGo(); }
public void UsingThreadJoin() { // create an instance ThreadsShareInstanceVaraibles tt = new ThreadsShareInstanceVaraibles(); Task t = new Task(tt.ThreadUnsafeGo); t.Start(); t.Wait(); // waits till t completes. That will have "done" set to true tt.ThreadUnsafeGo(); }
public void WhatIfForgroundThreadEndsBeforeBackgroundThread() { // create an instance ThreadsShareInstanceVaraibles tt = new ThreadsShareInstanceVaraibles(); Task t = Task.Factory.StartNew(tt.ThreadUnsafeGo) .ContinueWith((x) => Console.WriteLine("hello!")); t.Wait(); // waits till t completes. That will have "done" set to true tt.ThreadUnsafeGo(); Task t2 = Task.Factory.StartNew(() => { Thread.Sleep(10); Console.WriteLine("Tata !!"); }); //It may not print "Tata!!" is you do not wait for thread t2. }