-1

I 'm working on a big project. My form is a mdi container of form1. form1 loading is too slow and i can see the controls being drawn.

Every form have an update method but I still have the same problem. I can't use task to create new form.

this is my code

       form1.MdiParent = Me
       form1.Show()

my form have over 40 control (textbox , label , gridview ...) and yes it is heavy UI. Is there any solution to make use of the concurrence technology like: task, async await?

Muhammed Albarmavi
  • 23,240
  • 8
  • 66
  • 91
  • 2
    It is very difficult to figure out what you are asking. Do you have a specific question to make it a little easier? As it stands the question seems very broad and without some clarification, it will probably be closed. – djv Aug 26 '14 at 21:23
  • WinForms has been always slow while showing up a form with many controls in place, there are several things you can try to speed up the process, probably the most effective way is probably handle the WM_PAINT message from WndProc to avoid redraws, then when all controls are ready, you unblock the message and refresh your form. I do not include this as an answer because I'm not providing you the right code so I prefer just to comment because you will need to check different combinations (like double buffering and off-screen rendering + the WM_PAINT approach) to find the right way for you – Jesus Salas Aug 26 '14 at 21:40
  • @JesusSalas I am just curious. You block the `WM_PAINT`. Nothing is drawn. Then you refresh. Everything is drawn. How is that faster. Double buffering is not used for faster drawings but for smoother animation or avoiding flicker issues. And lastly, you dont have to block `WM_PAINT`. Just start with all controls visibility set to false. – γηράσκω δ' αεί πολλά διδασκόμε Aug 26 '14 at 21:56
  • 1
    @valter if I remember properly (I run into this same issue in a big scale project with WinForms years ago) the reason it is faster is related to every time you add a new control when setting up the form the form is invalidated causing a redraw for all child controls as well, then blocking the main form redraw until all controls are added to Children collection and everything is ready avoid this to happen speeding up the process, as I can't remember all the details on how I implemented this workaround I just wanted to provide some hints. I'll try to write some code to provide an exact answer. – Jesus Salas Aug 26 '14 at 22:26
  • Duplicate of [How to fix the flickering in User controls](https://stackoverflow.com/questions/2612487/how-to-fix-the-flickering-in-user-controls/2613272) –  Jan 07 '21 at 09:07

1 Answers1

8

After running some tests and look into some older code I have been able to locate something that helps on this particular issue, you can try to add next style to your form

public partial class Form1 : Form
{
    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams CP = base.CreateParams;
            CP.ExStyle = CP.ExStyle | 0x02000000; // WS_EX_COMPOSITED
            return CP;
        }
    }
}

You can take a look to WS_EX_COMPOSITED documentation here: http://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx

I tried this on a form with 752 child controls (labels, textboxes, datagrids, comboboxes and listboxes) and I can't see how child controls are draw anymore, they just splash into view.

VB version:

Protected Overrides ReadOnly Property CreateParams() As CreateParams
 Get
     Dim cp As CreateParams = MyBase.CreateParams
        cp.ExStyle = cp.ExStyle Or &H02000000 
     Return cp
 End Get
End Property
Jesus Salas
  • 652
  • 3
  • 14