private void Start_Ballon(object sender, RoutedEventArgs e) { // lancer un thread Ballon // distribuer un identifiant unique pour chaque window Ballon par attribut Uid // ajout handler pour l'évènement window.closed Thread th = new Thread(new ThreadStart(() => { Window viewer = new WindowBallon(); viewer.Uid = Convert.ToString(thread_index++); Debug.WriteLine(viewer.Uid); viewer.Closed += (s, evenetment) => { // arrêter le dispatcher quand le window est fermé Dispatcher.CurrentDispatcher.BeginInvokeShutdown(DispatcherPriority.Background); // handler de l'évènement Closed thread_exited_handler(s); }; viewer.Show(); Dispatcher.Run(); })); th.SetApartmentState(ApartmentState.STA); //th.IsBackground = true; th.Start(); nb_ballon++; threads.Add(th); // mise à jour la liste de threads thread_list.Add("Le thread n° " + Convert.ToString(index) + '\t' + "est de type Ballon et son ID est " + '\t' + Convert.ToString(th.ManagedThreadId)); index++; // mise à jour le Textbox qui indique le nombre de threads Ballon lancés TextNbBallon.Text = Convert.ToString(nb_ballon); }
//lauch a new ballon process private void Ballon_Click(object sender, RoutedEventArgs e) { Thread th = new Thread(delegate() { Window window = new WindowBallon(); window.Show(); window.Closed += (s, evenetment) => { System.Windows.Threading.Dispatcher.ExitAllFrames(); Ballon_exit(); }; System.Windows.Threading.Dispatcher.Run(); }); th.SetApartmentState(ApartmentState.STA);; th.Start(); num_ballon++; processList.Add("new process of ballon" + Convert.ToString(num_ballon) + "\t" + Convert.ToString(th.ManagedThreadId)); threads.Add(th); }
/* --------------- AUXILIARY FUNCTIONS ---------------- */ // launch a thread with name thread_name, // append it to threads list and refresh table private void launch_thread(string thread_name) { void launch_thread_ball() { WindowBallon b = new WindowBallon(); b.Show(); System.Windows.Threading.Dispatcher.Run(); } void launch_thread_prime() { NombrePremier p = new NombrePremier(); p.CalculatePrimes(); System.Windows.Threading.Dispatcher.Run(); } Thread t = (thread_name == BALL_DLL_NAME) ? new Thread(launch_thread_ball) : new Thread(launch_thread_prime); t.Name = thread_name; // thread must be STA to change UI components t.SetApartmentState(ApartmentState.STA); t.Start(); // add exit callback // t.EnableRaisingEvents = true; // Application.ThreadExit += new EventHandler(child_thread_exited); // t.ThreadExit += new EventHandler(child_thread_exited); // append thread to list threads.Add(t); update_listView(); }