Cycle de progression Faire (quelque chose qui marche) -> comprendre ce que l’on fait/comment cela marche -> pousser plus loin les notions
| //1 on declare un thread System.Threading.Thread oThread; |
| // 2 on instancie le thread + nom de la méthode qui devra être appelé par ce thread oThread = new System.Threading.Thread(DoWork); // 3 on lance le thread oThread.Start(); |
| ///<summary> /// 4 méthode ///</summary> public void DoWork() { } |
| Join() | Bloquer le thread (2 surcharges) |
| Abort() | Arreter le thread |
| Suspend() | Suspend execution du thread |
| Resume() | Reprend execution du thread |
| IsAlive | Indique si le thread est en cours d’execution (true) |
| IsBackgound | Indique si le thread est un thread arriere-plan |
| IsThreadPoolThread | Indique si c’est un thread de pool |
| Priority | Priorité du thread |
| ManagedThreadId | Identificateur du thread Ex : Les méthodes appelées par le délégué s’exécutent dans un thread secondaire (attention aux opérations inter-threads) (il est possible de le constater en faisant appel à System.Threading.Thread.CurrentThread.ManagedThreadId [identificateur du thread ex :7]) |
| public partial class FAsynchronousThread : Form { public FAsynchronousThread() { InitializeComponent(); } //1 on declare un thread System.Threading.Thread oThread; // EventHandler de la progression public delegate void PostDataEventHandler(int i); private void button1_Click(object sender, EventArgs e) { // 2 on instancie le thread + nom de la méthode qui devra être appelé par ce thread oThread = new System.Threading.Thread(DoWork); // 3 on lance le thread oThread.Start(); } ///<summary> /// méthode qui simule un traitement long ///</summary> public void DoWork() { int nDuration = 0; for (int i = 0; i <= 5; i++) { System.Threading.Thread.Sleep(500); nDuration += 500; // 4 methode qui permettra d'atteindre les controls de la form sans avoir de probleme d'acces inter-thread this.Invoke(new PostDataEventHandler(PostData), i); } } public void PostData(int i) { this.progressBar1.PerformStep(); } private void FAsynchronousThread_Load(object sender, EventArgs e) { // //this.progressBar1.Maximum = 5; //this.progressBar1.Step = 1; //this.progressBar1.Value = 0; } } |
| // 1 public delegate void ActionHandler(); // 2 public ActionHandler oActionHandler; |
| // 3 on definie la méthode appelée par le delegué oActionHandler = new ActionHandler(DoWork); // 4 on lance oActionHandler.BeginInvoke(null, null); |
| ///<summary> /// 4 méthode ///</summary> public void DoWork() { } |
| public partial class FAsynchronousDelegateOK : Form { public FAsynchronousDelegateOK() { InitializeComponent(); } // 1 public delegate void ActionHandler(); // 2 public ActionHandler oActionHandler; // EventHandler de la progression public delegate void PostDataEventHandler(int i); private void button1_Click(object sender, EventArgs e) { // 3 on definie la méthode appelée par le delegué oActionHandler = new ActionHandler(DoWork); // 4 on lance oActionHandler.BeginInvoke(null, null); } ///<summary> /// méthode qui simule un traitement long ///</summary> public void DoWork() { int nDuration = 0; for (int i = 0; i <= 5; i++) { System.Threading.Thread.Sleep(500); nDuration += 500; // 4 methode qui permettra d'atteindre les controls de la form sans avoir de probleme d'acces inter-thread this.Invoke(new PostDataEventHandler(PostData), i); } } public void PostData(int i) { this.progressBar1.PerformStep(); } } |
| // 1 declaration public BackgroundWorker oBackgoundWorker; |
| // 2 instanciation oBackgoundWorker = new BackgroundWorker(); // 3 // définie la method que le background execute en tache de fond oBackgoundWorker.DoWork += new DoWorkEventHandler(oBackgoundWorker_DoWork); // ne pas oublier afin de pouvoir reporter la progression oBackgoundWorker.WorkerReportsProgress = true; // définie la method appelée afin de notifier la progression du travail oBackgoundWorker.ProgressChanged += new ProgressChangedEventHandler(oBackgoundWorker_ProgressChanged); // définie la method appelée lorsque le travail est terminé oBackgoundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(oBackgoundWorker_RunWorkerCompleted); // 4 lance oBackgoundWorker.RunWorkerAsync(); |
| void oBackgoundWorker_DoWork(object sender, DoWorkEventArgs e) { } void oBackgoundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { } void oBackgoundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { } |
| public partial class FBackgroudworker : Form { public FBackgroudworker() { InitializeComponent(); } // 1 declaration public BackgroundWorker oBackgoundWorker; private void button1_Click(object sender, EventArgs e) { // 2 instanciation oBackgoundWorker = new BackgroundWorker(); // 3 // définie la method que le background execute en tache de fond oBackgoundWorker.DoWork += new DoWorkEventHandler(oBackgoundWorker_DoWork); // ne pas oublier afin de pouvoir reporter la progression oBackgoundWorker.WorkerReportsProgress = true; // définie la method appelée afin de notifier la progression du travail oBackgoundWorker.ProgressChanged += new ProgressChangedEventHandler(oBackgoundWorker_ProgressChanged); // définie la method appelée lorsque le travail est terminé oBackgoundWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(oBackgoundWorker_RunWorkerCompleted); // 4 lance oBackgoundWorker.RunWorkerAsync(); } void oBackgoundWorker_DoWork(object sender, DoWorkEventArgs e) { this.DoWork(); } void oBackgoundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { this.progressBar1.PerformStep(); } void oBackgoundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { MessageBox.Show("Fini !!"); } ///<summary> /// méthode qui simule un traitement long ///</summary> public void DoWork() { int nDuration = 0; for (int i = 0; i <= 5; i++) { System.Threading.Thread.Sleep(500); nDuration += 500; // repotre la progression oBackgoundWorker.ReportProgress(i * 20); } } } |