delegate int GetLength();
public int GetValue()
{
if (this.richTextBox1.InvokeRequired)
{
GetLength g = new GetLength(GetValue);
this.Invoke(g);
return richTextBox1.Lines.Length;//这里会报错:从不是创建控件“richTextBox1”的线程访问它
}
else
{
return richTextBox1.Lines.Length;
}
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(getValue));
thread.Start(); } public void getValue()
{
GetValue();
}
我用委托为什么会出错呢,是什么原因。给richTextBox1赋值都没有错,从里面取值为什么会出错呢?
public int GetValue()
{
if (this.richTextBox1.InvokeRequired)
{
GetLength g = new GetLength(GetValue);
this.Invoke(g);
return richTextBox1.Lines.Length;//这里会报错:从不是创建控件“richTextBox1”的线程访问它
}
else
{
return richTextBox1.Lines.Length;
}
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(getValue));
thread.Start(); } public void getValue()
{
GetValue();
}
我用委托为什么会出错呢,是什么原因。给richTextBox1赋值都没有错,从里面取值为什么会出错呢?
public int GetValue()
{
int re = 0;
if (this.InvokeRequired)
{
GetLength g = GetTextLength;
re =(int)this.Invoke(g);
}
else
{
re = GetTextLength();
}
return re;
} int GetTextLength()
{
return richTextBox1.Lines.Length;
}
}
else
{
return richTextBox1.Lines.Length;
} 这2句是一样的。没有用到Invoke