Avoid calling Invoke when the control is disposed

后端 未结 14 1341
花落未央
花落未央 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.

    0 讨论(0)
  • 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
    }
    
    0 讨论(0)
  • 2020-12-01 16:21

    The suggestion to stop the thread generating the messages is not acceptable. Delegates can be multicast. Because one listener does not want to listen to the band, you don't shoot the band members. Since the framework doesn't provide any easy way I know of to clear the message pump of those event messages, and since the form does not expose its private property that lets us know the form is closing: Set a flag on the IsClosing Event of the window after you unsubscribe or stop listening to the events, and always check this flag before you do a this.Invoke().

    0 讨论(0)
  • 2020-12-01 16:22

    The reality is that with Invoke and friends, you can't completely protect against invoke on a disposed component, or then getting InvalidOperationException because of the missing handle. I haven't really seen an answer yet, like the one farther below, in any of the threads that addresses the real fundamental problem, which cant be completely solved by preemptive testing or using lock semantics.

    Here's the normal 'correct' idiom:

    // the event handler. in this case preped for cross thread calls  
    void OnEventMyUpdate(object sender, MyUpdateEventArgs e)
    {
        if (!this.IsHandleCreated) return;  // ignore events if we arn't ready, and for
                                            // invoke if cant listen to msg queue anyway
        if (InvokeRequired) 
            Invoke(new MyUpdateCallback(this.MyUpdate), e.MyData);
        else
            this.MyUpdate(e.MyData);
    }
    
    // the update function
    void MyUpdate(Object myData)
    {
        ...
    }
    

    The fundemental problem:

    In using the Invoke facility the windows message queue is used, which places a message in the queue to either wait or fire-and-forget the cross thread call exactly like Post or Send message. If there is a message ahead of the Invoke message that will invalidate the component and its window handle, or that got placed just after any checks you try to perform, then you are going to have a bad time.

     x thread -> PostMessage(WM_CLOSE);   // put 'WM_CLOSE' in queue
     y thread -> this.IsHandleCreated     // yes we have a valid handle
     y thread -> this.Invoke();           // put 'Invoke' in queue
    ui thread -> this.Destroy();          // Close processed, handle gone
     y thread -> throw Invalid....()      // 'Send' comes back, thrown on calling thread y
    

    There is no real way to know that the control is about to remove itself fromthe queue, and nothing really reasonable you can do to "undo" the invoke. No matter how many checks you do or extra locks you make, you cant stop someone else form issuing something like a close, or deactivate. There are tons of senarios where this can happen.

    A solution:

    The first thing to realize is that the invoke is going to fail, no different than how a (IsHandleCreated) check would have ignored the event. If the goal is to protect the caller on the non-UI thread you will need to handle the exception, and treat it like any other call that didn't succeed (to keep app from crashing or do whatever. And unless going to rewrite/reroll Invoke facility, the catch is your only way to know.

    // the event handler. in this case preped for cross thread calls  
    void OnEventMyWhatever(object sender, MyUpdateEventArgs e)
    {
        if (!this.IsHandleCreated) return;
        if (InvokeRequired) 
        {
            try
            {
                Invoke(new MyUpdateCallback(this.MyUpdate), e.MyData);
            }
            catch (InvalidOperationException ex)    // pump died before we were processed
            {
                if (this.IsHandleCreated) throw;    // not the droids we are looking for
            }
        }
        else
        {
            this.MyUpdate(e.MyData);
        }
    }
    
    // the update function
    void MyUpdate(Object myData)
    {
        ...
    }
    

    The exception filtering can be tailored to suit whatever the needs are. Its good to be aware that worker threads often dont have all the cushy outer exception handling and logging the UI threads do, in most applicaitons, so you may wish to just gobble up any exception on the worker side. Or log and rethrow all of them. For many, uncaught exceptions on worker thread means the app is going to crash.

    0 讨论(0)
  • 2020-12-01 16:22

    Handle the Form closing event. Check to see if your off UI thread work is still happening, if so start to bring it down, cancel the closing event and then reschedule the close using BeginInvoke on the form control.

    private void Form_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (service.IsRunning)
        {
            service.Exit();
            e.Cancel = true;
            this.BeginInvoke(new Action(() => { this.Close(); }));
        }
    }
    
    0 讨论(0)
  • 2020-12-01 16:23

    i have same error. my error occurred in thread. finally i write this method :

    public bool IsDisposed(Control ctrl)
    {
        if (ctrl.IsDisposed)
            return true;
        try
        {
            ctrl.Invoke(new Action(() => { }));
            return false;
        }
        catch (ObjectDisposedException)
        {
            return true;
        }
    }
    
    0 讨论(0)
提交回复
热议问题