Interesting questions...

  • Thread starter Thread starter babylon
  • Start date Start date
B

babylon

Suppose I have a Form "MyForm", it has a function called void CallMeBack();
I show the Form using "Form.ShowDialog()"
when the form shows, it register the CallMeBack function to a timer event
which expires in 10 mins
then I Close/Dispose the Form before the timer expires...

when the timer expires, will it be able to call the "CallMeBack"?
since MyForm no longer exist....

========
what if MyForm.CallMeBack() is located inside a DLL and it is loaded
dynamically, display MyForm, register CallMeBack(), Close/Dispose MyForm,
Unload the DLL, timer expires....?

thank you

Regards,
Baron
 
I'm not a CLR expert, but here is my guess...

It depends on where the Timer is scoped. If it's part of the form that's
closed and goes out of scope, then it'll go out of scope and be garbage
collected before it fires. If the Timer is scoped elsewhere, then the
delegate reference is kept live and the Form isn't garbage collected and the
method is called when the Timer fires.
 
Hi babylon,

Here is what happens. When you place a timer on you form using the designer
the form creates the timer object using
Timer(IContainer) constructor overload. This adds the timer to the forms
components collection.
When you close the form Form's Dispose method is called and it disposes all
objects in the component collection and that's why your timer stops ticking.

If you want to have your timer keep ticking you don't have to dispose it.
Do do that the easiest way is not to use the designer, but create the timer
object by yourself in the form's constructor let's say using the Timer()
construcor (the overload without parameters). After that set all properties
and add the Tick event handler manually.

Now when you close the form the form is going to be disposed, but not the
timer because it is no longer in the form's components collection. You will
have your *CallMeBack* method called on every tick. Be careful the form is
disposed so don't do anything that may need to have Form's Handler created.

Until the timer is ticking the framework keeps a reference to the timer
internally so it is not eligible for garbage collection. The timer itself
keeps reference to the form (via Tick event) so the form won't be collected
as well, even though you may not keep any references to the form in the
application. Assuming that you have broken all references to the for the
only way to make the form object garbage is to stop the timer using any of
the timer's methods: Dispose, Stop or Enabled = false. At this point the
form and all its controls and components are garbage. Again, the last is in
case that you don't keep reference to the form anywhere in your application.
 
So, even I call Form.Dispose(), as long as the timer is ticking (so it keeps
a reference to the Form)
the Form isn't really Disposed?!

Ic what you mean.

However,
let's say if we are running a C++ program.
1. DLL A contains Function1
2. Program X load DLL A
3. Program X Start a timer and put Function1 as the Callback function when
the timer expires
4. Program X unload DLL A
5. Timer Expire...

what will happen?

thank you
 
I won't a simple win32 program myself
The TIMERPROC is located in a dll

If i don't call FreeLibrary to free the dll before the timer expires,
it does call the TIMERPROC...
however, if I call FreeLibraryto free the dll before the timer expires,
it doesn't call the TIMERPROC...

So..I C#, do Dispose works like calling FreeLibrary, which works like the
'code' is free/disposed...

thx
 
babylon,
So, even I call Form.Dispose(), as long as the timer is ticking (so it keeps
a reference to the Form)
the Form isn't really Disposed?!

No the form is fully disposed, but the timer is not. Disposed form means
that the unmanaged resources are released like there is no longer valid
Handle (HWND).
WindowsForms Form class is something like a wrapper around a native Windows
window. After disposing the form that native window gets destroyed and no
longer exists hence, you can't call any function or use property that uses
internaly the native window. But your managed wrapping object exists and it
is normal alive object as long as you keep reference to it. If the timer is
ticking the framework keeps reference to the timer object and the timer
keeps reference to the form object that's way your form object is not a
garbage (but rememer the form is disposed so no valid HWND exisits so the
usage of the form is limited)
Ic what you mean.

However,
let's say if we are running a C++ program.
1. DLL A contains Function1
2. Program X load DLL A
3. Program X Start a timer and put Function1 as the Callback function when
the timer expires
4. Program X unload DLL A
5. Timer Expire...
what will happen?
Well, it depends. You know that Win32 code is not safe so I can't tell what
exactly will happen. You know that the dlls keep count of loadin/unloading
operations. So you may unload the library but it may still remain in the
memory. According to your next post the timer stops calling the callback
when the unload the dll . I don't know how this happens since the SetTimer
method receives only a memory address. Maybe windows is to smart to stop the
timer. Frankly, I don't know. And I haven't tested it.
How do you know the timer doesn't keep calling that address?
 
Back
Top