1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
using System.Threading;
public class Thread : System.Windows.Forms.Form
{
// ........
// Evènement de signal de fin de thread
private AutoResetEvnet _endThreadCalculsEvent = new AutoResetEvent(false);
// Pour arrêter le thread
private void Button2_Click(Object sender, System.EventArgs e)
{
// L'evenement passe à l'état signalé
_endThreadCalculsEvent.Set();
// On attend la fin du thread.
_threadCalculs1.Join();
}
// Fonction du thread
private void ThrFunc1()
{
try
{
Calculs(1000);
}
Catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
private void Calculs(int tempor)
{
// Si l'évenement est à l'état signalé, WaitOne renvoie true et la boucle se termine.
while (! _endThreadCalculsEvent.WaitOne(tempo, false) )
{
// C'est ici ou notre thread fait son travail
// .....
}
}
} |