Fermer tout les thread d'un programme ?

Résolu
cs_Sobieski Messages postés 54 Date d'inscription mardi 22 mars 2005 Statut Membre Dernière intervention 13 mars 2007 - 5 juil. 2005 à 10:22
Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 - 24 août 2014 à 12:39
Salut.
J'ai un programme, quand je le lance ca lance une IHM et plein de thread qui filent des infos à l'ihm pour l'affichage.
Quand je clique sur la croix je ferme que la fenetre et j'ai toujours le process de fond qui tourne avec les threads genre client TCP etc...
Comment faire pour que quand je clique sur la croix ca kill le process de mon appli ?
Si je peux pas redefinir la méthode appelée par la croix, si je fais un bouton quitter, je met quoi derrière pour que ca kill le process ?

Merci d'avance !

8 réponses

popeyeSharp Messages postés 1 Date d'inscription dimanche 24 août 2014 Statut Membre Dernière intervention 24 août 2014 2
24 août 2014 à 12:12
Environment.Exit(0);

Cette fonction tue ton processus ainsi que tous ses threads
3
Whismeril Messages postés 19026 Date d'inscription mardi 11 mars 2003 Statut Contributeur Dernière intervention 20 avril 2024 656
24 août 2014 à 12:39
On peut espérer que depuis 9 ans il ait trouvé...
0
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
5 juil. 2005 à 10:29
Quand tu ferme ta form, l'evement Closing (FormClosing en .net 2 ) est
déclenché, il t'appartient donc de t'y inscrire et d'y arreter tes
threads avec la méthode Abort();

Tu peux aussi définir la propriété IsBackground de tes threads a true, pour qu'ils ne bloquent pas la fermeture de ton appli.

Mx
1
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
5 juil. 2005 à 10:35
Pour l'inscription a un évenement, tu fais comme ceci :

Dans ton InitializeComponent (ou autre):



this.Closing += new System.ComponentModel.CancelEventHandler(MainForm_Closing);


puis tu crées une méthode

private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)

{

// Et la tu fais la fermeture de tes threads

}
Mx
1
cs_Sobieski Messages postés 54 Date d'inscription mardi 22 mars 2005 Statut Membre Dernière intervention 13 mars 2007
5 juil. 2005 à 10:33
Je vais plutôt changer la propriété de mes threads vu que je suis un peu noob en C# je sais pas trop comment ça marche l'inscription à un evenement.
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_Sobieski Messages postés 54 Date d'inscription mardi 22 mars 2005 Statut Membre Dernière intervention 13 mars 2007
Modifié par Whismeril le 24/08/2014 à 12:41
En fait j'ai comme dans l'idée que mon programme est assez mal fait (honte à moi).
En gros j'ai une IHM genre Form1.cs :

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;


namespace CenzubIHM
{
 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.PictureBox pictureBox1;
  private System.Windows.Forms.PictureBox pictureBox2;
  private System.Windows.Forms.PictureBox pictureBox3;
  private System.Windows.Forms.PictureBox pictureBox4;
  private System.Windows.Forms.Button button1;
  public string str1="lol";
  public string str2="i";
  public string str3="lol";
  public System.Windows.Forms.TextBox textBox1;
  public System.Windows.Forms.TextBox textBox2;
  public System.Windows.Forms.TextBox textBox3;
  public System.Windows.Forms.PictureBox pictureBox5;
  public System.Windows.Forms.PictureBox pictureBox6;
  public System.Windows.Forms.PictureBox pictureBox7;
  public ComSFXFront _sfx;
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;


  public Form1(ComSFXFront sfx)
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();
   this._sfx=sfx;


   //
   // TODO: Add any constructor code after InitializeComponent call
   //
  }


  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null) 
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }


  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
   this.pictureBox1 = new System.Windows.Forms.PictureBox();
   this.pictureBox2 = new System.Windows.Forms.PictureBox();
   this.pictureBox3 = new System.Windows.Forms.PictureBox();
   this.pictureBox4 = new System.Windows.Forms.PictureBox();
   this.button1 = new System.Windows.Forms.Button();
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.textBox2 = new System.Windows.Forms.TextBox();
   this.textBox3 = new System.Windows.Forms.TextBox();
   this.pictureBox5 = new System.Windows.Forms.PictureBox();
   this.pictureBox6 = new System.Windows.Forms.PictureBox();
   this.pictureBox7 = new System.Windows.Forms.PictureBox();
   this.SuspendLayout();
   // 
   // pictureBox1
   // 
   this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
   this.pictureBox1.Location = new System.Drawing.Point(120, 280);
   this.pictureBox1.Name = "pictureBox1";
   this.pictureBox1.Size = new System.Drawing.Size(176, 72);
   this.pictureBox1.TabIndex = 0;
   this.pictureBox1.TabStop = false;
   // 
   // pictureBox2
   // 
   this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
   this.pictureBox2.Location = new System.Drawing.Point(120, 352);
   this.pictureBox2.Name = "pictureBox2";
   this.pictureBox2.Size = new System.Drawing.Size(176, 72);
   this.pictureBox2.TabIndex = 1;
   this.pictureBox2.TabStop = false;
   // 
   // pictureBox3
   // 
   this.pictureBox3.BackColor = System.Drawing.SystemColors.Control;
   this.pictureBox3.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox3.Image")));
   this.pictureBox3.Location = new System.Drawing.Point(112, 32);
   this.pictureBox3.Name = "pictureBox3";
   this.pictureBox3.Size = new System.Drawing.Size(176, 72);
   this.pictureBox3.TabIndex = 2;
   this.pictureBox3.TabStop = false;
   // 
   // pictureBox4
   // 
   this.pictureBox4.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox4.Image")));
   this.pictureBox4.Location = new System.Drawing.Point(112, 104);
   this.pictureBox4.Name = "pictureBox4";
   this.pictureBox4.Size = new System.Drawing.Size(176, 72);
   this.pictureBox4.TabIndex = 3;
   this.pictureBox4.TabStop = false;
   // 
   // button1
   // 
   this.button1.Location = new System.Drawing.Point(376, 8);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(56, 24);
   this.button1.TabIndex = 4;
   this.button1.Text = "sfx";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   // 
   // textBox1
   // 
   this.textBox1.Location = new System.Drawing.Point(88, 464);
   this.textBox1.Name = "textBox1";
   this.textBox1.ReadOnly = true;
   this.textBox1.Size = new System.Drawing.Size(224, 20);
   this.textBox1.TabIndex = 5;
   this.textBox1.Text = "";
   // 
   // textBox2
   // 
   this.textBox2.Location = new System.Drawing.Point(88, 488);
   this.textBox2.Name = "textBox2";
   this.textBox2.ReadOnly = true;
   this.textBox2.Size = new System.Drawing.Size(224, 20);
   this.textBox2.TabIndex = 6;
   this.textBox2.Text = "";
   // 
   // textBox3
   // 
   this.textBox3.Location = new System.Drawing.Point(88, 512);
   this.textBox3.Name = "textBox3";
   this.textBox3.ReadOnly = true;
   this.textBox3.Size = new System.Drawing.Size(224, 20);
   this.textBox3.TabIndex = 7;
   this.textBox3.Text = "";
   // 
   // pictureBox5
   // 
   this.pictureBox5.Location = new System.Drawing.Point(320, 472);
   this.pictureBox5.Name = "pictureBox5";
   this.pictureBox5.Size = new System.Drawing.Size(24, 16);
   this.pictureBox5.TabIndex = 8;
   this.pictureBox5.TabStop = false;
   // 
   // pictureBox6
   // 
   this.pictureBox6.Location = new System.Drawing.Point(320, 496);
   this.pictureBox6.Name = "pictureBox6";
   this.pictureBox6.Size = new System.Drawing.Size(24, 16);
   this.pictureBox6.TabIndex = 9;
   this.pictureBox6.TabStop = false;
   // 
   // pictureBox7
   // 
   this.pictureBox7.Location = new System.Drawing.Point(320, 520);
   this.pictureBox7.Name = "pictureBox7";
   this.pictureBox7.Size = new System.Drawing.Size(24, 16);
   this.pictureBox7.TabIndex = 10;
   this.pictureBox7.TabStop = false;
   // 
   // Form1
   // 
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(440, 613);
   this.Controls.Add(this.pictureBox7);
   this.Controls.Add(this.pictureBox6);
   this.Controls.Add(this.pictureBox5);
   this.Controls.Add(this.textBox3);
   this.Controls.Add(this.textBox2);
   this.Controls.Add(this.textBox1);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.pictureBox4);
   this.Controls.Add(this.pictureBox3);
   this.Controls.Add(this.pictureBox2);
   this.Controls.Add(this.pictureBox1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);


  }
  #endregion


  private void button1_Click(object sender, System.EventArgs e)
  {
   Telecommande tc = new Telecommande(this._sfx);
   tc.Show();
  }
 }
}

Mais pour lancer tout ca j'utilise un truc genre Class1.cs :

using System;
using System.Windows.Forms;
using System.Threading;


namespace CenzubIHM
{
 /// <summary>
 /// Summary description for Main.
 /// </summary>
 public class Class1
 {
  [STAThread]
  static void Main(string[] args)
  {
   ComSFXFront sfx = new ComSFXFront();
   this.sfx.Connect();
   Thread.Sleep(300);
   Form1 form1 = new Form1(sfx);
   ComIHM cihm = new ComIHM(ref form1);
   Thread client = new Thread(new ThreadStart(cihm.Start));
   client.Start();
   Application.Run(form1);
  }
 }
}


Donc en gros je transmet a form1 tout mes threads pour les fermer avec la méthode MainForm_Closing ?

EDIT coloration syntaxique
0
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
5 juil. 2005 à 11:04
Je dirais qu'il faudrait plutot que tu mettes tes threads dans la
classe Form1 (ou dans ComIHM ?) et que tu les lances depuis celle ci.

Maintenant, c'est assez compliqué de comprendre la structure du programme la :/ donc si ca se trouve, je me trompe complétement.



Mx
0
Lutinore Messages postés 3246 Date d'inscription lundi 25 avril 2005 Statut Membre Dernière intervention 27 octobre 2012 41
5 juil. 2005 à 11:07
Salut, tu peux aussi fermer les threads dans la méthode dispose..

protected override void Dispose( bool disposing )
{
try
{
// Ici..
}
finally
{
base.Dispose( disposing );
}
}
0
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
5 juil. 2005 à 11:09
Moi j'aime bien chercher compliqué

Mx
0
Rejoignez-nous