我的代码中出现错误:
跨线程操作无效:控件“listBox1”从创建它的线程以外的线程访问
代码:
private void button2_Click(object sender, EventArgs e)
{
Thread t1 = new Thread(mult);
t1.Start();
}
public void mult()
{
FileStream fq = new FileStream(textBox1.Text, FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);
StreamReader s = new StreamReader(fq);
while (!s.EndOfStream)
{
Thread.Sleep(500);
listBox1.Items.Add(s.ReadLine()); //error at this line
}
s.Close();