非同期で同じインスタンス内の変数を操作
private static Generater stagene; private void ExecThread() { stagene = new Generater(); int cnt = 12; Thread[] thread = new Thread[cnt]; for (int i = 0; i < cnt; i++) thread[i] = new Thread(this.ExceDo); for (int i = 0; i < cnt; i++) thread[i].Start(); Thread.Sleep(300); Console.WriteLine(Generater.Count.ToString()); } private void ExceDo() { Generater.Count++; }
対象クラス
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
public class Generater { public static int Count; Generater s_Instance; public Generater() { s_Instance = this; } }
}