サンプル内容
- 処理開始とともにダイアログを表示
- スレッドを10個作成
- スレッドが全て終了後、ダイアログを閉じる
サンプル
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace MultiThread
{
public partial class Form1 : Form
{
public static int count = 0;
public static int max = 9;
public delegate void Exec(int id);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var f = new Form2();
f.ShowDialog(this);
Exec d = Execute;
for (int i = 0; i <= Form1.max; i++)
{
d.BeginInvoke(i, (iresult) => {
if (++Form1.count == Form1.max)
{
f.Invoke((MethodInvoker)(() => { f.Close(); }));
Form1.count = 0;
}
}, null);
}
}
public static void Execute(int id)
{
Thread.Sleep(1000);
}
}
}