我在這行代碼得到一個InvalidOperationException
:運行時錯誤儘管使用調用
SynchronizedInvoke(this,() => this.Opacity = this.Opacity + 0.05);
要具體介紹一下運行時錯誤...
跨線程操作無效:控制'frmToaster'從其創建線程以外的線程訪問。
我用SynchronizedInvoke
方法,建議在Stack Overflow上的其他地方,以確保需要而定的調用使用。
static void SynchronizedInvoke(ISynchronizeInvoke sync, Action action)
{
// If the invoke is not required, then invoke here and get out.
if (!sync.InvokeRequired)
{
// Execute action.
action();
// Get out.
return;
}
// Marshal to the required thread.
sync.Invoke(action, new object[] { });
}
這段代碼如何生成,即使我用SynchronizeInvoke
避免它有關跨線運行時錯誤調用?