我想在线程中生成 3 个随机数,然后将其保存到列表中。这是我的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.IO;
namespace Multithreading1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
List<int> myList = new List<int>();
int threadNumber = 0;
int currentRecNumber = -1;
public MainWindow()
{
InitializeComponent();
}
void ThreadHandler(int recNumber,int number)
{
Action action = null;
action = () =>
{
myList[recNumber] = number;
++currentRecNumber;
--threadNumber;
if (currentRecNumber < myList.Count)
{
++threadNumber;
Thread t = new Thread(() => GetRandomNumber(currentRecNumber));
t.Start();
}
else
if (threadNumber == 0) //finish
{
List<String> stringList = new List<String>();
for (int i = 0; i < myList.Count;i++)
{
stringList.Add(myList[i].ToString());
}
File.WriteAllLines("C:\\Users\\Public\\Documents\\MyList.txt", stringList);
System.Windows.MessageBox.Show("Finish");
}
};
this.Dispatcher.BeginInvoke(action);
}
void GetRandomNumber(int recNumber)
{
Random rnd = new Random();
int randomInt = rnd.Next(1, 13);
ThreadHandler(recNumber, randomInt);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i < 20; i++)
{
myList.Add(-1);
}
for (int i = 0; i < 3; i++)
{
++currentRecNumber;
++threadNumber;
Thread t = new Thread(() => GetRandomNumber(currentRecNumber));
t.Start();
}
}
}
}
问题是: 1. 有时它会在 myList[recNumber] = number 处抛出 ArgumentOutOfRangeException;2. 如果超过 (1),则生成的文件仍然包含 -1,例如:
-1
-1
8
6
11
-1
1
3
-1
3
3
8
8
8
8
10
10
10
10
12
有谁知道出了什么问题?提前致谢。