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 45 46 47 48 49 50 51 52 53 54 55 56 57
| using System;
using System.Threading;
// En .Net 1.x
namespace Threads
{
public class MonThread
{
int i = 0;
public MonThread(int i)
{
this.i = i;
}
public void ThreadFonction()
{
for(; i<=30; i++)
{
Console.WriteLine(i.ToString());
Thread.Sleep(300);
}
}
}
public class Exemple
{
public static void Main()
{
MonThread mt1 = new MonThread(5);
Thread t1 = new Thread(new ThreadStart(mt1.ThreadFonction));
t1.Start();
MonThread mt2 = new MonThread(10);
Thread t2 = new Thread(new ThreadStart(mt2.ThreadFonction));
t2.Start();
}
}
}
// En .Net 2
// Il suffit d'utiliser ParameterizedThreadStart
using System;
using System.Threading;
public class Exemple
{
public static void Main()
{
Exemple e = new Exemple();
}
private Thread t;
public Exemple()
{
t = new Thread(new ParameterizedThreadStart(IncrementToTen));
t.Start(1);// Le parametre est passé dans la méthode Start()
}
public void IncrementToTen(object temp)
{
int i = (int)temp;
while(i < 10) i++;
Console.WriteLine(i.ToString());
}
} |
Partager