【发布时间】:2017-08-10 19:06:44
【问题描述】:
我正在使用SharpAdbClient 并注册了一个名为OnDeviceConnected 的事件,该事件在设备插入运行我的表单的PC 时触发。
当设备连接并触发此事件时,我正在尝试像这样设置 PictureBox 图像:
void OnDeviceConnected(object sender, DeviceDataEventArgs e)
{
...
imgBootFlashState.Image = Properties.Resources.locked; // CRASHES
helpBootState.Image = Properties.Resources.help_boot_flash_disabled; // CRASHES
...
}
此尝试引发此错误:
Cross-thread operation not valid: Control 'MainForm' accessed from a thread
other than the thread it was created on.
我不知道这个“其他”线程来自哪里,因为它不是来自我,但是如果我像这样更改 PictureBox 的 BackgroundImage:
void OnDeviceConnected(object sender, DeviceDataEventArgs e)
{
...
imgBootFlashState.BackgroundImage = Properties.Resources.locked; // WORKS
helpBootState.BackgroundImage = Properties.Resources.help_boot_flash_disabled; // WORKS
...
}
效果很好。
怎么可能?这个错误是怎么回事?
我知道我可以通过使用 BackgroundImage 属性来解决它,但我想了解是什么引发了这个错误......?
【问题讨论】:
标签: c# multithreading winforms