有人可以帮我吗?我遇到了一个问题。我正在尝试通过线程池在后台运行此代码,但似乎无法使其正常运行,我一直得到以下错误:
Cross-thread operation not valid: Control 'ListBox3' accessed
from a thread other than the thread it was created on.
这是我正在使用的代码:
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";
}
}
}
}
}