An application that dynamically adds controls to the Windows Form in a loop has a delayed response on the Microsoft Windows 98 operating system (814739)



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Windows 98 Second Edition

SYMPTOMS

If you create an application that dynamically adds controls to the Microsoft Windows Form in a loop, and then you run the application on a computer that is running the Microsoft Windows 98 operating system, the application does not respond for few minutes.

CAUSE

The controls that are not in use are disposed implicitly with the Controls.Dispose method. Controls.Dispose places the WM_CLOSE messages in the message queue. Because the controls are added and removed in a loop, these WM_CLOSE messages are not processed until the loop exits. When the loop exits, all the WM_CLOSE messages are processed at the same time. Therefore, the application stops responding for several minutes.

RESOLUTION

To resolve this issue, follow these steps:
  1. Add the following statement to the code after the Panel1.Controls.Add statement.

    Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET code
    'Processes all Windows messages that are currently in the message queue.
    Application.DoEvents()
    Microsoft Visual C# .NET code
    //Processes all Windows messages that are currently in the message queue.
     Application.DoEvents();
  2. On the Build menu, click Build Solution.
  3. Copy the recompiled executable that you created in the previous step to the target computer, and then test it.

STATUS

This behavior is by design.

MORE INFORMATION

The Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET IDE only runs on a computer that is running the Microsoft Windows 2000 operating system or a later version. Therefore, it is a good idea to build the sample application on a computer that is running Windows 2000 or a later version of Windows. Install the Microsoft .NET Framework on a computer that is running Windows 98. Then, copy the executable file that you built on Windows 2000 or a later version of Windows.

Steps to reproduce the behavior

  1. In Visual Studio 2005 or in Visual Studio .NET, start a new Windows application by using Visual Basic 2005, Visual Basic .NET, or Visual C# .NET.

    By default, Form1 is created.
  2. Add a Button control to Form1.
  3. Add a Panel control to Form1.
  4. In Solution Explorer, right-click WindowsApplication1, point to Add, and then click Add User Control.
  5. In the Add New Item dialog box, click Open.
  6. Add a Label control to UserControl1.
  7. On Form1, add the following code to the Click event of Button1.

    Visual Basic 2005 or Visual Basic .NET code
    Dim x, y As Integer
    For y = 1 To 50
       'Clear the controls created on Panel1.
        Panel1.Controls.Clear()
        For x = 0 To 10
            'Create an instance of UserControl1.
            Dim n As New UserControl1()
            n.Label1.Text = x
            n.Dock = System.Windows.Forms.DockStyle.Top
            'Add the control to the panel.
            Panel1.Controls.Add(n)
         Next
         System.Console.WriteLine(y.ToString())
     Next
    Visual C# .NET code
    int x,y;
    for(y = 1;y<=50;y++)
    {
         //Clear the controls created on Panel1.
         panel1.Controls.Clear();
         for (x=1; x <=20; x++)
             { 
                  //Create an instance of UserControl1.
                  UserControl1 c = new UserControl1();
                  c.label1.Text=x.ToString() ;
                  c.Dock =System.Windows.Forms.DockStyle.Top ;
                  //Add the control to the panel.
                  panel1.Controls.Add(c);
              }
         Console.WriteLine(y.ToString());
    }
  8. In Solution Explorer, right-click Windows Application1, and then click Properties.
  9. Under Output Type, click to select Console Application, and then click OK.
  10. On the Build menu, click Build Solution.
  11. Locate WindowsApplication1.exe under the Bin subfolder of the WindowsApplication1 project.
  12. Copy WindowsApplication1.exe to the computer that is running Windows 98.
  13. Right-click WindowsApplication1.exe, and then click Open.
  14. Click Button1.

    Notice the output in the console.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web sites:

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbDeployment kbsetup kbForms kbDynamic kbWindowsForms kbCtrlCreate kbCtrl kbControl kbprb KB814739 kbAudDeveloper