【发布时间】:2010-12-08 08:32:54
【问题描述】:
在我的 Windows 窗体中,我有一个文本框和一个按钮,文本框“tb_LogBox”是多行文本框我编译并运行它 Visual Studio 抛出 InvalidOperationException。
我得到的实际错误 跨线程操作无效:控件 'tb_LogBox' 从创建它的线程以外的线程访问。以下示例代码说明了我正在尝试做的事情
private void button1_Click(object sender, EventArgs e)
{
try
{
var bw = new BackgroundWorker();
bw.DoWork += ExecuteOperations ;
bw.RunWorkerAsync();
}
catch (Exception ex)
{
tb_LogBox.AppendText(Environment.NewLine + " =@= " + ex.Message+" "+ex.Source);
}
}
private void ExecuteOperations(object sender, DoWorkEventArgs e)
{
var FuncCall = new LogTimer();
tb_LogBox.AppendText(Environment.NewLine + FuncCall.DnT()); // the line i am getting the error. on
}
public class LogTimer
{
public string DnT()
{
const string datePat = @"d/MM/yyyy";
var dateTime = DateTime.Now();
return dateTime.ToString(datePat);
}
}
【问题讨论】:
标签: c# .net visual-studio