我想將MainForm中的線程從2個變量傳遞到另一個窗體,但是這樣做時會發生錯誤。使用C將線程變量從線程傳遞到另一個窗體#
private void TrackingThread()
{
float targetX = 0;
float targetY = 0;
while (true)
{
camera1Acquired.WaitOne();
camera2Acquired.WaitOne();
lock (this)
{
// stop the thread if it was signaled
if ((x1 == -1) && (y1 == -1) && (x2 == -1) && (y2 == -1))
{
break;
}
// get middle point
targetX = (x1 + x2)/2;
targetY = (y1 + y2)/2;
}
if (directionForm != null)
{
directionForm.RunMotors(targetX, targetY);
}
}
}
在形式directionForm中,我只是顯示變量targetX和targetY。 的directionForm.RunMotors()的代碼如下:
public void RunMotors(float x, float y)
{
label1.Text = "X-ordinate " + x.ToString();
label2.Text = "Y-ordinate " + y.ToString();
}
當我試圖顯示兩個變量中的錯誤發生: 「InvalidOperationException異常是未處理 跨線程操作無效:控制LABEL1訪問從一個線以外的線創建線「
我該怎麼辦?
在此先感謝
@kirstom:請標記您的問題是否已被回答。 – 2010-04-08 08:08:21