Re: ObjectDisposedException by gol
gol
Thu Jul 10 02:03:01 CDT 2008
Thank you all very much for your answers,
I'll try to explain my problem in more details. The way I see it, the timer
is not the problem, and actually I don't need the timer.
I am receiving an event in a delegate. The event is received correctly, on
time, exactly as expected. As soon as receiving the event I want to make the
button visible. This didn't succeed so I thought of using a flag and a timer
(because I remember once using that method worked well for me). This didn't
solve the problem.
The basic problem remained the same: When reopening the form, and receiving
the event, the button is already disposed. I tried to change the button's
visibility in the constructor and there it succeeds also in the second time.
(There is no exception).
I thought about it a lot, and there is something that I want to ask if this
may cause this problem. In the class where I receive the event I also have an
instance of the class that fires the event. However, it's defined as static.
So I think that on the second time of event firing, it's still the first
firing class trying to fire the event to the second receiving class, in which
it is not defined. Is this true?
Is there a way to keep that instance static and make the other things work
correctly?
What bothers me regarding this is that I use the same event receiving
mechanism for a different event and it works perfect. And another thing that
I don't understand- suppose I am using the timer, after entering the
timer_tick event handler the button should be the one that belongs to the
instance of the class that is now running, regardless of the event firing
class. Is this true?
I forgot to mention that I work in .Net Compact Framework 2.0.
Thank you all very much
"cody" wrote:
> gol wrote:
> > Hi,
> > I have an invisible button in my form. I have a timer
> > (System.Windows.Forms.Timer) with interval 300 ms. In the timer tick event I
> > check for a certain flag, and if that flag is true, the button becomes
> > visible (After setting the flag's status to false again).
> >
> > The problem is that if the event happened and the button became visible, and
> > then I closed the form, and then reopened it and let the event happen again,
> > then the operation of making the button visible throws an exception of type
> > System.ObjectDisposedException.
> >
> > It looks like when I reopen the form, I already haven't got the button. One
> > could think it's been Garbage Collected, but I'm not sure about that, because
> > the button is part of the form which is reopened (Form f = new Form(); ).
> >
> > Can someone please explain the source of the problem and the solution?
> > Thanks a lot
> >
>
> You must Stop() & Dispose() the timer before closing the form.
> Otherwise, the timer still runs although the Form is already disposed.
>