精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
Error: "Cross-thread operation not valid: Control 'FileDownloaderForm' accessed from a thread other than the thread it was created on." in string: Error", MessageBoxButtons.OK, MessageBoxIcon.Error);"
从非控件创建线程之外线程访问控件时,报错:跨线程操作无效。字符串:"MessageBoxButtons.OK, MessageBoxIcon.Error);"
public static void Publish(Exception ex)
{
IWin32Window owner = null;
if(downloaderForm != null && downloaderForm.Visible)
owner = downloaderForm;
MessageBox.Show(downloaderForm, ex.ToString(), "Download Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
downloaderForm.status = DownloadStatus.Failed;
downloaderForm.canceled = true;
downloaderForm.btnCancel.Text = "&Close";
downloaderForm.btnPause.Visible = false;
}
Use CheckForIllegalCrossThreadCalls = false;
使用CheckForIllegalCrossThreadCalls = false;
Add a class level delegate to the FileDownloadForm like this:
添加一个类级别委托给FileDownloadForm,像这样的:
delegate void PublishEventHandler(Exception ex);
On the FileDownloadForm, whenever you publish an exception you should check to see if an invoke is required:
在FileDownloadForm中,每当你发布一个异常,你应该检查是否需要invoke:
catch(Exception ex)
{
if(this.InvokeRequired)
{
this.Invoke(new PublishEventHandler(ExceptionManager.Publish),
new object[]{ex});
}
else
ExceptionManager.Publish(ex);
}
There are several other ways to implement this, but basically that'll fix it.
还有其他几种方法来实现这一点,但是这个方法基本上就能解决它。