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
|
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public delegate void mydelegate(string message);
public Form1(){
InitializeComponent();
}
private void MiseAJourAff(string a){
Form1.listBox1.Items.Add(a);
}
public void ThreadProc(){
for (int i = 0; i < 10; i++)
{
//Console.WriteLine("ThreadProc: {0}", i);
this.Invoke(new mydelegate(MiseAJourAff), "ThreadProc: {0}" + i);
Thread.Sleep(100);
}
}
public void button1_Click(object sender, EventArgs e){
//Console.WriteLine("Main thread: Start a second thread.");
MiseAJourAff("Main thread: Start a second thread.");//Console.WriteLine("Main thread: Start a second thread.");
Thread t = new Thread(new ThreadStart(ThreadProc));
t.Start();
for (int i = 0; i < 4; i++){
Console.WriteLine("Main thread: Do some work.");
Thread.Sleep(10);
t.Join();
}
Console.WriteLine("Main thread: Call Join(), to wait until ThreadProc ends.");
t.Join();
Console.WriteLine("Main thread: ThreadProc.Join has returned. Press Enter to end program.");
Console.ReadLine();
}
}
} |
Partager