在Windows窗体中,跨线程操作无效 [英] Cross-thread operation not valid in Windows Forms
本文介绍了在Windows窗体中,跨线程操作无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
跨线程操作无效:控制'ListBox3'从其创建线程以外的线程访问
。
以下是我使用的代码:
private void DoWork(object o)
{
var list = ListBox3;
var request = createRequest(TxtServer.Text,WebRequestMethods.Ftp.ListDirectory);
using(var response =(FtpWebResponse)request.GetResponse())
{
using(var stream = response.GetResponseStream())
{
使用(var reader = new StreamReader(stream,true))
{
while(!reader.EndOfStream)
{
list.Items.Add(reader.ReadLine() );
ResultLabel.Text =连接;
}
}
}
}
}
解决方案
您需要调用委托来更新列表。请参阅此 MSDN示例。
Could anyone help me i have a problem I'm trying to get this code to work in the background via threadpool but i cannot seem to get it to work i keep getting this error:
Cross-thread operation not valid: Control 'ListBox3' accessed
from a thread other than the thread it was created on.
Here is the code i am using:
private void DoWork(object o)
{
var list = ListBox3;
var request = createRequest(TxtServer.Text, WebRequestMethods.Ftp.ListDirectory);
using (var response = (FtpWebResponse)request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
using (var reader = new StreamReader(stream, true))
{
while (!reader.EndOfStream)
{
list.Items.Add(reader.ReadLine());
ResultLabel.Text = "Connected";
}
}
}
}
}
解决方案
You need to invoke a delegate to update the list. See this MSDN example.
这篇关于在Windows窗体中,跨线程操作无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文