C#中关于线程的问题! mythread.start();后面加上mythread.join(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 源代码:就是一个抓取淘宝下拉框词的程序: private void taobao_old_key(List<string> tmp) { foreach (string keyword in tmp) { //此处代码较长,以dosomthing代替,主要是获取源码的过程,没有使用到任何控件 dosomthing; //下面的是使用正则表达式匹配源码,找到数据,添加到listbox控件当中 Regex taobaoold_regex = new Regex(@"\[""(?<taobaoold>.*?)"".*?"""); MatchCollection taobaoold_match = taobaoold_regex.Matches(mystringbuilder.ToString()); List<string> matches = new List<string>(); for (int i = 0; i < 10 && i < taobaoold_match.Count; i++) { matches.Add(taobaoold_match[i].Groups[1].Value); } AddToListBox(DroplistBox, matches); } } private void AddToListBox(ListBox listbox, string match) { if (listbox.InvokeRequired) { this.Invoke(new MethodInvoker(() => { AddToListBox(listbox, match); })); } else { listbox.Items.Add(match); } } private void Form1_Load(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false; } private void Startbutton_Click(object sender, EventArgs e) { List<string> listString; //对liststring赋值操作不写了,直接略过,然后将liststring的值传递到函数中去,就是挖掘liststring中关键词的数据 Start = new Thread(() => { taobao_old_key(listString); }); Start.IsBackground = true; Start.Start(); Start.Join(); MessageBox.Show("查询完毕!"); }就是这样的一段代码,点击按钮之后,软件直接停住了,什么数据都不显示,去掉join()k就可以继续搜索了 源代码:就是一个抓取淘宝下拉框词的程序: private void taobao_old_key(List<string> tmp) { foreach (string keyword in tmp) { //此处代码较长,以dosomthing代替,主要是获取源码的过程,没有使用到任何控件 dosomthing; //下面的是使用正则表达式匹配源码,找到数据,添加到listbox控件当中 Regex taobaoold_regex = new Regex(@"\[""(?<taobaoold>.*?)"".*?"""); MatchCollection taobaoold_match = taobaoold_regex.Matches(mystringbuilder.ToString()); List<string> matches = new List<string>(); for (int i = 0; i < 10 && i < taobaoold_match.Count; i++) { matches.Add(taobaoold_match[i].Groups[1].Value); } AddToListBox(DroplistBox, matches); } } private void AddToListBox(ListBox listbox, List<string> matches) { if (listbox.InvokeRequired) { this.Invoke(new MethodInvoker(() => { AddToListBox(listbox, match); })); } else { foreach (string match in matches) { listbox.Items.Add(match); } } } private void Form1_Load(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false; } private void Startbutton_Click(object sender, EventArgs e) { List<string> listString; //对liststring赋值操作不写了,直接略过,然后将liststring的值传递到函数中去,就是挖掘liststring中关键词的数据 Start = new Thread(() => { taobao_old_key(listString); }); Start.IsBackground = true; Start.Start(); Start.Join(); MessageBox.Show("查询完毕!"); }就是这样的一段代码,点击按钮之后,软件直接停住了,什么数据都不显示,去掉join()k就可以继续搜索了下面的这个是正确的代码,上面的代码有些问题! 源代码:就是一个抓取淘宝下拉框词的程序: private void taobao_old_key(List<string> tmp) { foreach (string keyword in tmp) { //此处代码较长,以dosomthing代替,主要是获取源码的过程,没有使用到任何控件 dosomthing; //下面的是使用正则表达式匹配源码,找到数据,添加到listbox控件当中 Regex taobaoold_regex = new Regex(@"\[""(?<taobaoold>.*?)"".*?"""); MatchCollection taobaoold_match = taobaoold_regex.Matches(mystringbuilder.ToString()); List<string> matches = new List<string>(); for (int i = 0; i < 10 && i < taobaoold_match.Count; i++) { matches.Add(taobaoold_match[i].Groups[1].Value); } AddToListBox(DroplistBox, matches); } } private void AddToListBox(ListBox listbox, List<string> matches) { if (listbox.InvokeRequired) { this.Invoke(new MethodInvoker(() => { AddToListBox(listbox, match); })); } else { foreach (string match in matches) { listbox.Items.Add(match); } } } private void Form1_Load(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false; } private void Startbutton_Click(object sender, EventArgs e) { List<string> listString; //对liststring赋值操作不写了,直接略过,然后将liststring的值传递到函数中去,就是挖掘liststring中关键词的数据 Start = new Thread(() => { taobao_old_key(listString); }); Start.IsBackground = true; Start.Start(); Start.Join(); MessageBox.Show("查询完毕!"); }就是这样的一段代码,点击按钮之后,软件直接停住了,什么数据都不显示,去掉join()k就可以继续搜索了下面的这个是正确的代码,上面的代码有些问题!求高手指点为何会卡主 lz你不看msdn吗,join 就是block的。它等待线程结束才会继续执行。Thread.Join MethodBlocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage pumping.http://msdn.microsoft.com/en-us/library/95hbf2ta(v=vs.110).aspx lz可以看看这里的例程BackgroundWorker Classhttp://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx 主要是我不知道我遇到的问题叫什么,不知道如何在MSDN搜索呀! http://www.albahari.com/threading/ Join会阻塞在taobao_old_key线程中,而taobao_old_key线程又想通过委托回到UI线程更新,这样肯定会出现界面假死啦。最简单的改法是将MessageBox.Show("查询完毕!");移到下面位置: private void taobao_old_key(List<string> tmp) { foreach (string keyword in tmp) { //此处代码较长,以dosomthing代替,主要是获取源码的过程,没有使用到任何控件 dosomthing; //下面的是使用正则表达式匹配源码,找到数据,添加到listbox控件当中 Regex taobaoold_regex = new Regex(@"\[""(?<taobaoold>.*?)"".*?"""); MatchCollection taobaoold_match = taobaoold_regex.Matches(mystringbuilder.ToString()); List<string> matches = new List<string>(); for (int i = 0; i < 10 && i < taobaoold_match.Count; i++) { matches.Add(taobaoold_match[i].Groups[1].Value); } AddToListBox(DroplistBox, matches); } MessageBox.Show("查询完毕!"); } http://www.cnblogs.com/mashang/archive/2009/08/01/1536730.htmlBeginInvoke 和 Invoke 的区别 对了,修改的时候记得注释掉Join。 private void Startbutton_Click(object sender, EventArgs e) { List<string> listString; //对liststring赋值操作不写了,直接略过,然后将liststring的值传递到函数中去,就是挖掘liststring中关键词的数据 Start = new Thread(() => { taobao_old_key(listString); MessageBox.Show("查询完毕!"); }); Start.IsBackground = true; Start.Start(); } WinForm listView控件的高度调节 C#查询SQL数据库行数问题。 如何在更新语句中加入 "-"????????????????????????? 窗体内的time控件,为什么禁止掉之后就不能再开始了? C#中当前窗体的问题!!!!!急!急! this在什么时候用,有什么作用??? 30求救,高手请进:如何在MDI中打开一个已知名字的窗体.. String.Format的一个小问题 请问一个关于程序启动和窗体的问题 c#发送数据 【求助】关于win7和XP获取本机IP地址不同的问题 100分求:如何访问局域网的数据库?
{
foreach (string keyword in tmp)
{
//此处代码较长,以dosomthing代替,主要是获取源码的过程,没有使用到任何控件
dosomthing;
//下面的是使用正则表达式匹配源码,找到数据,添加到listbox控件当中
Regex taobaoold_regex = new Regex(@"\[""(?<taobaoold>.*?)"".*?""");
MatchCollection taobaoold_match = taobaoold_regex.Matches(mystringbuilder.ToString());
List<string> matches = new List<string>();
for (int i = 0; i < 10 && i < taobaoold_match.Count; i++)
{
matches.Add(taobaoold_match[i].Groups[1].Value);
}
AddToListBox(DroplistBox, matches);
}
}
private void AddToListBox(ListBox listbox, string match)
{
if (listbox.InvokeRequired)
{
this.Invoke(new MethodInvoker(() =>
{
AddToListBox(listbox, match);
}));
}
else
{
listbox.Items.Add(match);
}
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
private void Startbutton_Click(object sender, EventArgs e)
{
List<string> listString;
//对liststring赋值操作不写了,直接略过,然后将liststring的值传递到函数中去,就是挖掘liststring中关键词的数据
Start = new Thread(() =>
{
taobao_old_key(listString);
});
Start.IsBackground = true;
Start.Start();
Start.Join();
MessageBox.Show("查询完毕!");
}就是这样的一段代码,点击按钮之后,软件直接停住了,什么数据都不显示,去掉join()k就可以继续搜索了
{
foreach (string keyword in tmp)
{
//此处代码较长,以dosomthing代替,主要是获取源码的过程,没有使用到任何控件
dosomthing;
//下面的是使用正则表达式匹配源码,找到数据,添加到listbox控件当中
Regex taobaoold_regex = new Regex(@"\[""(?<taobaoold>.*?)"".*?""");
MatchCollection taobaoold_match = taobaoold_regex.Matches(mystringbuilder.ToString());
List<string> matches = new List<string>();
for (int i = 0; i < 10 && i < taobaoold_match.Count; i++)
{
matches.Add(taobaoold_match[i].Groups[1].Value);
}
AddToListBox(DroplistBox, matches);
}
}
private void AddToListBox(ListBox listbox, List<string> matches)
{
if (listbox.InvokeRequired)
{
this.Invoke(new MethodInvoker(() =>
{
AddToListBox(listbox, match);
}));
}
else
{
foreach (string match in matches)
{
listbox.Items.Add(match);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
private void Startbutton_Click(object sender, EventArgs e)
{
List<string> listString;
//对liststring赋值操作不写了,直接略过,然后将liststring的值传递到函数中去,就是挖掘liststring中关键词的数据
Start = new Thread(() =>
{
taobao_old_key(listString);
});
Start.IsBackground = true;
Start.Start();
Start.Join();
MessageBox.Show("查询完毕!");
}就是这样的一段代码,点击按钮之后,软件直接停住了,什么数据都不显示,去掉join()k就可以继续搜索了
下面的这个是正确的代码,上面的代码有些问题!
{
foreach (string keyword in tmp)
{
//此处代码较长,以dosomthing代替,主要是获取源码的过程,没有使用到任何控件
dosomthing;
//下面的是使用正则表达式匹配源码,找到数据,添加到listbox控件当中
Regex taobaoold_regex = new Regex(@"\[""(?<taobaoold>.*?)"".*?""");
MatchCollection taobaoold_match = taobaoold_regex.Matches(mystringbuilder.ToString());
List<string> matches = new List<string>();
for (int i = 0; i < 10 && i < taobaoold_match.Count; i++)
{
matches.Add(taobaoold_match[i].Groups[1].Value);
}
AddToListBox(DroplistBox, matches);
}
}
private void AddToListBox(ListBox listbox, List<string> matches)
{
if (listbox.InvokeRequired)
{
this.Invoke(new MethodInvoker(() =>
{
AddToListBox(listbox, match);
}));
}
else
{
foreach (string match in matches)
{
listbox.Items.Add(match);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
CheckForIllegalCrossThreadCalls = false;
}
private void Startbutton_Click(object sender, EventArgs e)
{
List<string> listString;
//对liststring赋值操作不写了,直接略过,然后将liststring的值传递到函数中去,就是挖掘liststring中关键词的数据
Start = new Thread(() =>
{
taobao_old_key(listString);
});
Start.IsBackground = true;
Start.Start();
Start.Join();
MessageBox.Show("查询完毕!");
}就是这样的一段代码,点击按钮之后,软件直接停住了,什么数据都不显示,去掉join()k就可以继续搜索了
下面的这个是正确的代码,上面的代码有些问题!求高手指点为何会卡主
Blocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage pumping.http://msdn.microsoft.com/en-us/library/95hbf2ta(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx
{
foreach (string keyword in tmp)
{
//此处代码较长,以dosomthing代替,主要是获取源码的过程,没有使用到任何控件
dosomthing;
//下面的是使用正则表达式匹配源码,找到数据,添加到listbox控件当中
Regex taobaoold_regex = new Regex(@"\[""(?<taobaoold>.*?)"".*?""");
MatchCollection taobaoold_match = taobaoold_regex.Matches(mystringbuilder.ToString());
List<string> matches = new List<string>();
for (int i = 0; i < 10 && i < taobaoold_match.Count; i++)
{
matches.Add(taobaoold_match[i].Groups[1].Value);
}
AddToListBox(DroplistBox, matches);
}
MessageBox.Show("查询完毕!");
}
BeginInvoke 和 Invoke 的区别
{
List<string> listString;
//对liststring赋值操作不写了,直接略过,然后将liststring的值传递到函数中去,就是挖掘liststring中关键词的数据
Start = new Thread(() =>
{
taobao_old_key(listString);
MessageBox.Show("查询完毕!");
});
Start.IsBackground = true;
Start.Start();
}