public partial class JobDataDuplicatorForm : Form
{
public JobDataDuplicatorForm(IJobDataDuplicatorEngine engine)
{
_engine.CopyStartedEvent += GetEventHandler(OnCopyStarted);
_engine.CopyEndedEvent += GetEventHandler(OnCopyEnded);
...
}
private static EventHandler GetEventHandler(Action action)
{
return (sender, args) => action();
}
private void OnCopyStarted()
{
copyStatus.Text = "Copy progress: ";
generateButton.Enabled = false; // Cross-thread operation not valid
}
}我有以下例外:
Additional information: Cross-thread operation not valid: Control
'generateButton' accessed from a thread other than the thread it was created on.我可以通过更改GetEventHandler()来修复异常,而不是像这样将每个按钮包装在不同的地方
Invoke((MethodInvoker)delegate { generateButton.Enabled = false; });?
我该怎么做?
发布于 2016-03-28 16:05:13
根据您的评论,您说您是从后台线程调用JobDataDuplicatorForm(IJobDataDuplicatorEngine engine)的。
您的类是一个Form,任何windows控件(包括表单)都必须首先在UI线程或诸如Invoke和InvokeRequired中断之类的东西上创建。无论调用什么代码,JobDataDuplicatorForm的构造函数都必须从UI线程执行该调用。
https://stackoverflow.com/questions/36265163
复制相似问题