私は別のスレッドからデータを移動したいが、私のコードは唯一の代わりにリストに最初の5つの値を保存し、その後 それらをプリントアウトの私が合格最初の値のために働くには、ここに私のコードです:スレッド間でデータを渡すにはどうすればよいですか?
private readonly ConcurrentQueue<int> _queue = new ConcurrentQueue<int>();
private readonly AutoResetEvent _signal = new AutoResetEvent(false);
public void Thread1()
{
List<int> values = new List<int>();
int lastInput;
StringBuilder sb = new StringBuilder();
while (values.Count < 5)
{
_signal.WaitOne();
_queue.TryDequeue(out lastInput);
values.Add(lastInput);
}
for (int i = 0; i < values.Count; i++)
{
sb.Append(String.Format("{0}\n", values[i]));
}
MessageBox.Show(sb.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
Thread th1 = new Thread(Thread1);
th1.Start();
for (int i = 0; i < 8; i++)
{
_queue.Enqueue(i);
_signal.Set();
}
}
備考欄[ここ](https://msdn.microsoft.com/en-us/library/system.threading.eventwaithandle.set(v = vs.110))に「重要」の注記がありますか? aspx)?あなたの 'Set()'コールのほとんどが何もしていないと思われます。 –
[BlockingCollection](https://msdn.microsoft.com/en-us/library/dd267312(英語))の独自のインプリメンテーションを構築しようとしているようです。 v = .110).aspx)を使用してください。 –