我对 C# 和所有线程的东西都很陌生,目前我收到“跨线程操作无效错误”。
以下是代码的相关部分:
private LinkedList<string> _statusList = new LinkedList<string>();
private void ReportToStatus(string message)
{
_statusList.AddLast(message);\
// textStatus is a textbox.
// And this is the exact line that is giving the error:
textStatus.Lines = _statusList.ToArray();
}
private void RunTest()
{
// ...
// Run the test in the background worker.
bgwTest.RunWorkerAsync(testCase);
}
private void bgwTest_DoWork(object sender, DoWorkEventArgs e)
{
TestCase testCase = e.Argument as TestCase;
// ...
// Run the test.
switch (testCase.TestType)
{
case "TestA": TestA(testCase);
break;
}
e.Result = testCase;
}
private void TestA(TestCase testCase)
{
// ...
PrintStatistic(statisticsForCoil, testCase.OutputFile);
}
}
private void PrintStatistic(int[] statistics, string outputFile)
{
// ...
ReportToStatus(result);
}
我应该如何进行?