我已經得到了這個錯誤:線程錯誤
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
Additional information: Cross-thread operation not valid: Control 'Redlight' accessed from a thread other than the thread it was created on.
紅燈和綠光是pictureBoxes。 基本上,我希望它能夠做的就是每秒鐘在每幅圖片之間交替。 我在這個網站搜索了類似的錯誤,我看到它與「調用」有關,但我甚至不知道那是什麼,有人能夠啓發我嗎?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace EMCTool
{
public partial class EMCTool_MainForm : Form
{
bool offOn = false;
public EMCTool_MainForm()
{
InitializeComponent();
}
private void EMCTool_MainForm_Load(object sender, EventArgs e)
{
System.Threading.Timer timer = new System.Threading.Timer(new System.Threading.TimerCallback(timerCallback), null, 0, 1000);
}
private void timerCallback(object obj)
{
if (offOn == false)
{
Redlight.Show();
offOn = true;
}
else
{
Greenlight.Show();
offOn = false;
}
}
}
}
哦好吧,這如何調用事工作,這是什麼我做什麼? 編輯:我測試過,它的工作。 – Evan
Invoke方法委託在其上創建的線程(即UI線程)上完成與UI相關的處理。 –
@ user1880591它將您的調用發送到創建控件的線程,並且該工作可以完成。 – Shaharyar