Avoid calling Invoke when the control is disposed

后端 未结 14 1445
花落未央
花落未央 2020-12-01 16:09

I have the following code in my worker thread (ImageListView below is derived from Control):

if (mImageListView != null &&          


        
14条回答
  •  执笔经年
    2020-12-01 16:17

    See also this question:

    Avoiding the woes of Invoke/BeginInvoke in cross-thread WinForm event handling?

    The utility class that resulted EventHandlerForControl can solve this problem for event method signatures. You could adapt this class or review the logic therein to solve the issue.

    The real problem here is that nobugz is correct as he points out that the APIs given for cross-thread calls in winforms are inherently not thread safe. Even within the calls to InvokeRequired and Invoke/BeginInvoke themselves there are several race conditions that can cause unexpected behavior.

提交回复
热议问题