当我使用组合框自动完成搜索时程序崩溃
Program crashed when i search using combobox autocomplete
我正在尝试使用组合框自动完成功能在数组变量中搜索具有大数据的数据。
这是我的作品:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread thread1;
long test1 = 0;
string randName;
String[] ComboboxValue = new String[20001];
private void button1_Click(object sender, EventArgs e)
{
thread1 = new Thread(add_combo_1);
thread1.Start();
}
private static Random random = new Random();
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
private void add_combo_1()
{
while (test1++ < 20000)
{
randName = RandomString(1000);
this.Invoke((MethodInvoker)delegate () {
//comboBox1.Items.Add(randName.ToString());
ComboboxValue[test1] = randName.ToString();
comboBox1.AutoCompleteCustomSource.Add(ComboboxValue[test1]);
});
}
}
}
这是我使用的组合框设置:
我认为当我搜索并且程序仍在将数据加载到数组时发生了崩溃。
所以有人有解决方案吗?
你必须让程序完成你设置的 while 循环,除非你在循环中插入一个 break;
有或没有 IF 语句
我正在尝试使用组合框自动完成功能在数组变量中搜索具有大数据的数据。
这是我的作品:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread thread1;
long test1 = 0;
string randName;
String[] ComboboxValue = new String[20001];
private void button1_Click(object sender, EventArgs e)
{
thread1 = new Thread(add_combo_1);
thread1.Start();
}
private static Random random = new Random();
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
private void add_combo_1()
{
while (test1++ < 20000)
{
randName = RandomString(1000);
this.Invoke((MethodInvoker)delegate () {
//comboBox1.Items.Add(randName.ToString());
ComboboxValue[test1] = randName.ToString();
comboBox1.AutoCompleteCustomSource.Add(ComboboxValue[test1]);
});
}
}
}
这是我使用的组合框设置:
我认为当我搜索并且程序仍在将数据加载到数组时发生了崩溃。
所以有人有解决方案吗?
你必须让程序完成你设置的 while 循环,除非你在循环中插入一个 break;
有或没有 IF 语句