Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Suspendre, Arrêter et reprendre un Thread


{filelink=17389}

using System;
using System.Threading; 
 
 public class SuspendreReprendreArret
 {
  public static void Main()
  {
    ExempleThread monThread = new ExempleThread("Thread #1");  
 
    Thread.Sleep(1000); 
 
    Console.WriteLine("Suspension de Thread");
    monThread.objetThread.Suspend();
    Thread.Sleep(1000); 
 
     Console.WriteLine("Reprise de thread.");
     monThread.objetThread.Resume();
    Thread.Sleep(1000); 
 
    Console.WriteLine("Arrêt de Thread.");
    monThread.objetThread.Abort(); 
 
    monThread.objetThread.Join(); 
 
    Console.WriteLine("Fin de Thread principal.");
  }
}
 
class ExempleThread
{
  public Thread objetThread;  
 
  public ExempleThread(string nom)
  {
    objetThread = new Thread(new ThreadStart(this.run));
    objetThread.Name = nom;
    objetThread.Start();
  }  
 
  void run() {
    Console.WriteLine(objetThread.Name + " Lancé."); 
 
    for(int i = 1; i <= 1000; i++) {
      Console.Write(i + " ");
      if((i%10)==0) {
        Console.WriteLine();
        Thread.Sleep(250);
      }
    }
    Console.WriteLine("Sorti de"+ objetThread.Name);
  }
}

No comments yet.

Leave a Comment