29

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

if (mImageListView != null && 
    mImageListView.IsHandleCreated &&
    !mImageListView.IsDisposed)
{
    if (mImageListView.InvokeRequired)
        mImageListView.Invoke(
            new RefreshDelegateInternal(mImageListView.RefreshInternal));
    else
        mImageListView.RefreshInternal();
}

However, I get an ObjectDisposedException sometimes with the Invoke method above. It appears that the control can be disposed between the time I check IsDisposed and I call Invoke. How can I avoid that?

Ozgur Ozcitak
  • 10,409
  • 8
  • 46
  • 56
  • Why is it disposed in the first place? – RvdK Dec 09 '09 at 15:51
  • @PoweRoy: I signal the threads to exit in the control's Dispose method. I know this was not the best thing to do, but I couldn't find a better place to signal the threads to exit. – Ozgur Ozcitak Dec 09 '09 at 16:06

14 Answers14

19

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
}
Isak Savo
  • 34,957
  • 11
  • 60
  • 92
  • I was really hoping to avoid try/catching. But I'll do that if it is the only solution. – Ozgur Ozcitak Dec 09 '09 at 15:50
  • Well you *can* solve it by using mutex or locks, but it is much more error prone and can lead to weird bugs as the code is evolving. You'll need to protect all calls to Dispose() with the same mutex and that'll get harder as the code evolves... – Isak Savo Dec 09 '09 at 15:59
14

There are implicit race conditions in your code. The control can be disposed between your IsDisposed test and the InvokeRequired test. There's another one between InvokeRequired and Invoke(). You can't fix this without ensuring the control outlives the life of the thread. Given that your thread is generating data for a list view, it ought to stop running before the list view disappears.

Do so by setting e.Cancel in the FormClosing event and signaling the thread to stop with a ManualResetEvent. When the thread completes, call Form.Close() again. Using BackgroundWorker makes it easy to implement the thread completion logic, find sample code in this post.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Isn't catching the `ObjectDisposedException` exception, as state below by Isak Savo, much cleaner than tinkering with the wicked Thread.Abort() or relying on the FormClosing event ? – arul Dec 09 '09 at 16:04
  • 2
    Can you guarantee it will only ever be an ObjectDisposedException? What if its a legitimate ObjectDisposedException? It's a rabbit-hole. Fix the problem, don't shoot the messenger. – Hans Passant Dec 09 '09 at 16:16
  • I ended up doing something similar to this. But instead of handling FormClosing, I overrode control's OnHandleDestroyed and blocked the UI thread while waiting for threads to exit. – Ozgur Ozcitak Dec 11 '09 at 14:58
  • 1
    This is normally a recipe for deadlock, a thread's completion callback can't run because the UI thread isn't pumping messages. Sounds like you managed to avoid it. – Hans Passant Dec 11 '09 at 16:18
  • Ouch. Keep banging on the X to make sure that works right. Your user certainly will. – Hans Passant Dec 11 '09 at 22:57
  • You are of course right. I can't say I'm proud of what I did to avoid the deadlock. I basically wrote something along the lines of: while(myThread.IsAlive) { Application.DoEvents(); } So I actually didn't block the UI thread, but merely prevented the Handle being destroyed until the worker threads are done. – Ozgur Ozcitak Dec 11 '09 at 22:58
  • Well, you are right but is that different from setting e.Cancel in FormClosing? – Ozgur Ozcitak Dec 11 '09 at 23:01
  • Yes, it doesn't recurse over and over again with bits of code executing after the recursion eventually returns. Mind you, I'm not saying it doesn't work, I'm just saying that it might byte you in the ass some day. I'm probably over-pessimistic, triggered by not seeing the code and the OP not using the suggested solutions. I trust you got it right. Even the soul that maintains your code after you leave might agree. – Hans Passant Dec 11 '09 at 23:15
  • You are probably right. As I said I'm not particularly proud of my solution. I wish there was an Control.InvokeIfYouCanOtherwiseIDontCare() method built in to the framework. Anyway, I'll reflect more on yours and others solutions. And here is the code if you'd be interested in checking it out: http://code.google.com/p/imagelistview/ – Ozgur Ozcitak Dec 12 '09 at 00:34
  • What's needed to do things cleanly are Control.TryBeginInvoke and Control.TryInvoke methods. Given the lack of them, I think the best approach is to write such a function yourself. Munching exceptions is icky, but it avoids a lot of added complexity. – supercat Oct 21 '10 at 19:13
  • Waiting for the threads to gracefully terminate themselves will most likely cause a delay in the form actually closing. This is poor user experience if the delay is too long. I need a more user experience friendly way. – goku_da_master Oct 30 '12 at 16:24
  • Hmm, threads that don't respond to a termination request in a timely manner is usually a design problem. It's not hard to hide that flaw. Call Hide(). – Hans Passant Oct 30 '12 at 17:27
  • I have the same issue in a User Control when it is disposed. Because I have no control on the closing parent form process, I had to override the Dispose method of the User Control and implement some strategy to wait the threads to stop gracefully. This is not enough because of pending Invokes in the UI Thread. The only way I found to flush them is to do an "Application.DoEvents()" at the very last moment. Then my UserControl disposes properly. It sounds horribly hacky, but it works fine so far. – Larry Jun 14 '13 at 16:49
4

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.

Beeeaaar
  • 1,010
  • 9
  • 19
  • 2
    I really wish there were `TryInvoke` and `TryBeginInvoke` methods for situations where an object is trying to do something like `Refresh` a control because its visible portions need to be updated to reflect new data. The desired post-condition is that the control not have any portions that would still need updating. The disposal of the control would imply that there are no visible portions, and thus no out-of-date visible portions; consequently, the post-condition would be satisfied, and the method should return successfully. – supercat Sep 06 '13 at 16:02
3

Try using

if(!myControl.Disposing)
    ; // invoke here

I had the exact same problem as you. Ever since I switched to checking .Disposing on the control, the ObjectDisposedException has gone away. Not saying this will fix it 100% of the time, just 99% ;) There is still a chance of a race condition between the check to Disposing and the call to invoke, but in the testing I've done I haven't ran into it (I use the ThreadPool and a worker thread).

Here's what I use before each call to invoke:

    private bool IsControlValid(Control myControl)
    {
        if (myControl == null) return false;
        if (myControl.IsDisposed) return false;
        if (myControl.Disposing) return false;
        if (!myControl.IsHandleCreated) return false;
        if (AbortThread) return false; // the signal to the thread to stop processing
        return true;
    }
goku_da_master
  • 4,257
  • 1
  • 41
  • 43
  • +1 to checking if IsHandleCreated is true/false. This doesn't fix the race condition, but is something to keep in mind. – TamusJRoyce Jul 16 '13 at 13:38
1

may be lock(mImageListView){...} ?

oldUser
  • 243
  • 2
  • 12
  • This won't work. There's no guarantee that Disposed isn't called while you are inside the lock. – Isak Savo Dec 09 '09 at 15:48
  • @Isak The purpose of a lock is to block other threads from accessing an object. If he locks the object, then by definition, it cannot be disposed while locked. – Jrud Dec 09 '09 at 15:50
  • You lock the object on Thread2, but the object gets disposed on Thread1. So either you introduce a deadlock, or a no-op. – arul Dec 09 '09 at 15:52
  • @Jrud: That's not true. Lock just means that you block other threads from trying to acquire the *same lock*. It is still possible to call any methods on a "locked" object. – Isak Savo Dec 09 '09 at 15:57
  • may be it will just prevent the GC from disposing it it, i guess it should check the state of SyncBlockIndex ob object before doing any activities(in case object's Dispose isn't called by user's code) – oldUser Dec 09 '09 at 16:28
1

You could use mutexes.

Somewhere at the start of the thread :

 Mutex m=new Mutex();

Then :

if (mImageListView != null && 
    mImageListView.IsHandleCreated &&
    !mImageListView.IsDisposed)
{
    m.WaitOne(); 

    if (mImageListView.InvokeRequired)
        mImageListView.Invoke(
            new RefreshDelegateInternal(mImageListView.RefreshInternal));
    else
        mImageListView.RefreshInternal();

    m.ReleaseMutex();
}

And whereever it is you are disposing of mImageListView :

 m.WaitOne(); 
 mImageListView.Dispose();
 m.ReleaseMutex();

This should ensure you cant dispose and invoke at the same time.

Mongus Pong
  • 11,337
  • 9
  • 44
  • 72
  • Isn't there still a race condition between the IsDisposed check and the WaitOne call? – Ozgur Ozcitak Dec 11 '09 at 14:55
  • Yes well spotted, you are right. You could either put another check for IsDisposed after the WaitOne call, or you could put the WaitOne before the IsDisposed check. I would choose the latter option if you anticipate IsDisposed to be false more times than true when the code is executed to save the extra call. – Mongus Pong Dec 11 '09 at 15:16
1

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.

Community
  • 1
  • 1
csharptest.net
  • 62,602
  • 11
  • 71
  • 89
1

If a BackGroundWorker is a possibility, there's a very simple way to circumvent this:

public partial class MyForm : Form
{
    private void InvokeViaBgw(Action action)
    {
        BGW.ReportProgress(0, action);
    }

    private void BGW_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        if (this.IsDisposed) return; //You are on the UI thread now, so no race condition

        var action = (Action)e.UserState;
        action();
    }

    private private void BGW_DoWork(object sender, DoWorkEventArgs e)
    {
       //Sample usage:
       this.InvokeViaBgw(() => MyTextBox.Text = "Foo");
    }
}
Community
  • 1
  • 1
Ohad Schneider
  • 36,600
  • 15
  • 168
  • 198
1

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(); }));
    }
}
Oliver
  • 2,175
  • 3
  • 24
  • 31
1

The solution proposed by Isak Savo

try
  {
  myForm.Invoke(myForm.myDelegate, new Object[] { message });
  }
catch (ObjectDisposedException)
  { //catch exception if the owner window is already closed
  }

works in C# 4.0 but for some reasons it fails in C#3.0 (the exception is raised anyway)

So I used another solution based on a flag indicating if the form is closing and consequently preventing the use of invoke if the flag is set

   public partial class Form1 : Form
   {
    bool _closing;
    public bool closing { get { return _closing; } }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        _closing = true;
    }

 ...

 // part executing in another thread: 

 if (_owner.closing == false)
  { // the invoke is skipped if the form is closing
  myForm.Invoke(myForm.myDelegate, new Object[] { message });
  }

This has the advantage of completely avoiding the use of try/catch.

Pierre Poliakoff
  • 5,305
  • 1
  • 15
  • 4
  • I think adding and removing eventhandlers and checking isDisposed is the most used way, but I gotta say. This worked perfectly for me. Getting rid of the try-catch and not having to use the events, all for the cost of one extra bool :-) Thanks for the hint! – Xan-Kun Clark-Davis Mar 16 '17 at 12:20
0

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().

rich
  • 1
0

One way might be to call the method itself ones more instead of invoking the ImageListView-Method:

if (mImageListView != null && 
    mImageListView.IsHandleCreated &&
    !mImageListView.IsDisposed)
{
    if (mImageListView.InvokeRequired)
        mImageListView.Invoke(new YourDelegate(thisMethod));
    else
        mImageListView.RefreshInternal();
}

That way it would check one more time before finally calling RefreshInternal().

Bobby
  • 11,419
  • 5
  • 44
  • 69
0

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;
    }
}
Think Big
  • 1,021
  • 14
  • 24
0

This works for me

if (this.IsHandleCreated){
    Task.Delay(500).ContinueWith(_ =>{
        this.Invoke(fm2);
    });
} else {
  this.Refresh();
}
Ryan Schaefer
  • 3,047
  • 1
  • 26
  • 46