Microsoft KB Archive/814739

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

Article ID: 814739

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * 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:   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;  On the Build menu, click Build Solution. 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
 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.</li> Add a Button control to Form1.</li> Add a Panel control to Form1.</li> In Solution Explorer, right-click WindowsApplication1, point to Add, and then click Add User Control.</li> In the Add New Item dialog box, click Open.</li> Add a Label control to UserControl1.</li>  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); } </li> In Solution Explorer, right-click Windows Application1, and then click Properties.</li> Under Output Type, click to select Console Application, and then click OK.</li> On the Build menu, click Build Solution.</li> Locate WindowsApplication1.exe under the Bin subfolder of the WindowsApplication1 project.</li> Copy WindowsApplication1.exe to the computer that is running Windows 98.</li> Right-click WindowsApplication1.exe, and then click Open.</li> Click Button1.

Notice the output in the console.</li></ol>

<div class="references_section">