1

I am creating a Windows form in C#

When I am adding a custom user form called Controller to the windows form. When I do this it calls the method Controller_Load. In this load method I am creating a task and I am starting it (it is in design mode my project is not run yet

private void Controller_Load(object sender, EventArgs e)
{    
    if (this.DesignMode)
    {               
        Task TestConnection = new Task(ConnectAsync);
        TestConnection.Start();               
    }  
}

The problem is I have the feeling that I can't run a task in design mode because my method ConnectAsync is never called. Is it true or am I just doing it wrong ?

I could also create a Thread but ConnectAsync is an asynchronous method so I chose to use tasks

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Manu
  • 51
  • 3
  • 1
    Check the make sure `DesignMode` is returning true. Apparently it is not that reliable. See [link](https://stackoverflow.com/a/10725662/2791540) for alternatives. – John Wu Mar 31 '22 at 15:48
  • The code that you have in Load event handler will not run when you open form, but the code that you have in constructor of the base class works. Take a look at [this post](https://stackoverflow.com/a/32299687/3110834) and see the example, to learn how designer works. – Reza Aghaei Mar 31 '22 at 16:21
  • You also need to add `async` to your `Controller_Load` signature and call the method like this `await ConnectAsync();` – Reza Aghaei Mar 31 '22 at 17:00
  • " I am adding a custom user form called Controller to the windows form"-this implies that you are adding a UserControl to the form. If this is correct then `Controller_Load` is the UC's load event handler and the value of DesignMode should be true. Ensure that `ConnectAsync` does not contain any bugs; remember that that code is running on a secondary thread and any interaction with control could cause a cross-thread issue. This interaction includes setting bindings or bindingsource values. – TnTinMn Apr 03 '22 at 20:01
  • Properly debug the UserControl design time code by following the instructions in [Walkthrough: Debug Custom Windows Forms Controls at Design Time]]https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/walkthrough-debugging-custom-windows-forms-controls-at-design-time?view=netframeworkdesktop-4.8). – TnTinMn Apr 03 '22 at 20:01

1 Answers1

2

The code that you have in the constructor of your Form will not run when you open it in designer, but the code that you have in constructor of the base class will run.

In fact, designer creates an instance of the base class of your form (runs constructor of the base) and deserializes the file which contains InitializeComponent method and then add the base class and the controls to the design surface.

You can take a look at the following post: How does Windows Forms Designer work? and see an interesting example there, where the designer loads a form definition full of error, but the designer forms.

In general if you have a design-time requirement, it's better to rely on the design-time extensibility point of IDE extensibility point. But just for learning purpose, in the following piece of code, you can see how the code of the base constructor runs in design time of the derived class:

  1. Create your base class like this:

    using System;
    using System.ComponentModel;
    using System.Windows.Forms;
    
    namespace SampleWinApp
    {
        public partial class BaseForm : Form
        {
            public BaseForm()
            {
                InitializeComponent();
                if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
                    MessageBox.Show("Hello from Constructor!");
    
            }
            private void BaseForm_Load(object sender, EventArgs e)
            {
                if (DesignMode)
                    MessageBox.Show("Hello from Base_Load!");
            }
        }
    }
    
  2. Create the derived class like this:

    using System;
    using System.Windows.Forms;
    
    namespace SampleWinApp
    {
        public partial class Form1 : BaseForm
        {
            public Form1()
            {
                InitializeComponent();
            }
        }
    }
    
  3. When you open Form1, you will the message box from Constructor and Load event handler of the base class.

Pay attention to the difference when checking for design time, in constructor and rest of the code.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398