Avoid calling Invoke when the control is disposed

后端 未结 14 1455
花落未央
花落未央 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:21

    What you have here is a race condition. You're better off just catching the ObjectDisposed exception and be done with it. In fact, I think in this case it is the only working solution.

    try
    {
        if (mImageListView.InvokeRequired)
           mImageListView.Invoke(new YourDelegate(thisMethod));
        else
           mImageListView.RefreshInternal();
    } 
    catch (ObjectDisposedException ex)
    {
        // Do something clever
    }
    

提交回复
热议问题