2

I have a System.Windows.Forms.Form like this:

public class MainForm : Form
{
    int _processProgress;
    public int ProcessProgress
    {
        get { return _processProgress; }
        set
        {
            _processProgress = value;
            if (ProcessProgressChanged != null)
                ProcessProgressChanged(value);
        }
    }

    public delegate void ProcessProgressChangedEventHandler(int progressPercentage);
    public event ProcessProgressChangedEventHandler ProcessProgressChanged;
}

And it has a UserControl like this:

public class MainFormControl : UserControl
{
    public MainFormControl()
    {
        ((MainForm)this.ParentForm).ProcessProgressChanged += (progPerc) =>
            {
                this.TextBox1.Text = "asd";
                // Do something
            };
    }
}

Will it unsubscribe the Anonymous Method from the constructor of the MainFormControl of the MainForm.ProcessProgressChanged event when the MainFormControl.Dispose() is called (or when the MainFormControl is removed from the MainForm)?

My code is in C#, framework 4, build in VS2010 Pro, project is in WinForms.

Please help. Thanks in advance.

John Isaiah Carmona
  • 5,260
  • 10
  • 45
  • 79

1 Answers1

1

No. but if the controls are on the form and the form is being disposed it really doesn't matter...

What you have to watch out for is when forms/controls hook up to back end services that are long running (singletons, etc.) You have to watch out there because the event can/will fire even when the control/form is disposed. If the handler does something that assumes the UI is still around (ie. not disposed) -- you run into trouble...

debracey
  • 6,517
  • 1
  • 30
  • 56
  • So Is it okay to run the `MainForm.ProcessProgressChanged` event even if the `MainFormControl` is disposed? – John Isaiah Carmona Mar 02 '12 at 02:41
  • Sounds like there's a misunderstanding here. When `MainForm` calls the `ProcessProgressChanged` event, your code (where it says `doSomething`) is called (on the same thread). If MainForm is disposing and calls the event, it will wait. If it is disposed, it shouldnt be firing events to being with. – debracey Mar 02 '12 at 02:49
  • But what if my code `doSomething` is using a control inside the `MainFormControl` (like `((MainFormControl)this).TextBox1.Text = "asd"`)? Is it okay to dispose the `MainFormControl` by the `MainForm` _(I do not dispose the form, what I dispose is the control of the form)_? – John Isaiah Carmona Mar 02 '12 at 02:55
  • Child components shouldn't access the ParentForm's controls. When you close the form it automatically disposes .... – debracey Mar 02 '12 at 03:05
  • The child component (`MainFormControl`) do not access the `ParentForm`'s (`MainForm`) control, what it access is the `ParentForm`'s event, that's why I add the `((MainFormControl)this)` for you to understand that it's accessing it's own controls. _I apologize for making my question(s) confusing._ I was just asking that what if the `Form` removes/dispose the `Control`, will it be okay to run the `Form.ProcessProgressChanged` event? – John Isaiah Carmona Mar 02 '12 at 03:13
  • When you dispose the form, all of its child controls also get disposed. If all the child controls do is connect to events on the parent form, there is no danger. After the main form is disposed, it wont be firing events unless you manually tell it to fire something, which you shouldnt do. – debracey Mar 02 '12 at 03:18
  • I really am sorry, and thanks for being enthusiastic in answering my questions, I swear that this will be my last question. I do not dispose the `Form`, what I dispose is the `Control` of the `Form`. And since the `Control` is disposed and the `Form.ProcessProgressChanged` event has been subscribed by the _Anonymous Method_ from the disposed `Control`, will it be okay to run the `Form.ProcessProgressChanged` event even if the _Anonymous Method_ is using the controls of the disposed `Control`? – John Isaiah Carmona Mar 02 '12 at 03:33
  • 1
    Ahh! Ok. In that case you need to disconnect the event in the dispose method (it's in the designer.cs file -- you can move it to the main cs file.) – debracey Mar 02 '12 at 03:52