Exemple #1
0
        public void CallMeFromMainThread()
        {
            // create an instance
            ThreadsShareInstanceVaraibles tt = new ThreadsShareInstanceVaraibles();
            Task t = new Task(tt.ThreadSafeGo);

            t.Start();
            tt.ThreadSafeGo();
        }
Exemple #2
0
        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();
        }
Exemple #3
0
        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.
        }