private void ballFinishedHandler(object sender, EventArgs e)
        {
            MyThread m = threads.Find(delegate(MyThread mythread)
            {
                return(mythread.getWorkerInstance().Equals((ThreadWorker)sender));
            });

            if (removeMyThread(m))
            {
                nbballThread--;
                updateTextblockDispatcher();
            }
        }
        private void closeLastPremier_click(object sender, RoutedEventArgs e)
        {
            MyThread m = threads.FindLast(delegate(MyThread mythread)
            {
                return(mythread.getWorkerInstanceType().IsAssignableFrom(typeof(PremierWorker)));
            });

            if (removeMyThread(m))
            {
                nbPremierThread--;
                updateTextblockDispatcher();
            }
        }
        private void closeAllThreads()
        {
            int nb = threads.Count();

            for (int i = nb - 1; i >= 0; i--)
            {
                MyThread t = threads.ElementAt(i);
                t.getThread().Abort();
                threads.Remove(t);
                Debug.WriteLine("closing : " + " i :" + i + t.GetType());
            }
            nbPremierThread = 0;
            nbballThread    = 0;
        }
        private void updateTextblock()
        {
            int count = threads.Count();

            textBlock.Text  = count + " threads are running : \n";
            textBlock.Text += "number of ballon threads : " + nbballThread + "\n";
            textBlock.Text += "number of premier threads : " + nbPremierThread + "\n";
            textBlock.Text += "********************************************************** \n";
            if (threads.Count != 0)
            {
                for (int i = 0; i < (threads.Count()); i++)
                {
                    MyThread m = threads.ElementAt(i);
                    textBlock.Text += "Thread number : " + i + " is a thread of " + (m.getWorkerInstanceType().IsAssignableFrom(typeof(PremierWorker)) ? "Premier" : "ballon") + " its id is: " + m.getThread().ManagedThreadId + "\n";
                }
            }
        }
        private void ballStart_click(object sender, RoutedEventArgs e)
        {
            if (threads.Count() < 5)
            {
                ThreadWorker ballonworker = new BallonWorker();
                ballonworker.workerFinishedEvent += ballFinishedHandler;
                Thread ballThread = new Thread(new ThreadStart(ballonworker.work));
                MyThread mythread = new MyThread(ballThread, ballonworker);
                threads.Add(mythread);
                ballThread.Start();
                nbballThread++;
                updateTextblockDispatcher();
            }
            else {
                System.Windows.MessageBox.Show("Already 5 threads have been created");
            }

        }
 private void ballStart_click(object sender, RoutedEventArgs e)
 {
     if (threads.Count() < 5)
     {
         ThreadWorker ballonworker = new BallonWorker();
         ballonworker.workerFinishedEvent += ballFinishedHandler;
         Thread   ballThread = new Thread(new ThreadStart(ballonworker.work));
         MyThread mythread   = new MyThread(ballThread, ballonworker);
         threads.Add(mythread);
         ballThread.Start();
         nbballThread++;
         updateTextblockDispatcher();
     }
     else
     {
         System.Windows.MessageBox.Show("Already 5 threads have been created");
     }
 }
        private bool removeMyThread(MyThread m)
        {
            if (m != null)
            {
                if (pause)
                {
#pragma warning disable CS0618 // Le type ou le membre est obsolète
                    m.getThread().Resume();
#pragma warning restore CS0618 // Le type ou le membre est obsolète
                }
                threads.Remove(m);
                m.getThread().Abort();
                if (threads.Count() == 0)
                {
                    pause = false;
                }
                return(true);
            }
            return(false);
        }
        private bool removeMyThread(MyThread m)
        {
            if (m != null)
            {
                if (pause)
                {
#pragma warning disable CS0618 // Le type ou le membre est obsolète
                    m.getThread().Resume();
#pragma warning restore CS0618 // Le type ou le membre est obsolète
                }
                threads.Remove(m);
                m.getThread().Abort();
                if (threads.Count() == 0) pause = false;
                return true;        
            }
            return false;
        }