1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Collections.Generic;
using System.Threading;
static class Program {
    static void Main() {
        Console.WriteLine("start.");
        WaitCallback func = delegate(object obj) {   // 実行されるタスク
            Console.WriteLine("start: {0}", obj);
            Thread.Sleep(new Random().Next(5, 10) * 1000);
            Console.WriteLine("finish: {0}", obj);
        };
        List<WaitHandle> waitHandles = new List<WaitHandle>();
        for(int i = 0; i < 10; i++) {
            waitHandles.Add(func.BeginInvoke(i, null, null).AsyncWaitHandle);
        }
        WaitHandle.WaitAll(waitHandles.ToArray());   // メインスレッドはここで待機
        Console.WriteLine("join.");
    }
}