Use double buffering technology to solve winform form control freezes (dataGridView is slow to load data)

1. Background

When working on a C/S project, the page loading control of the finished form was slow. After the form was enlarged or reduced, the form loading was stuck, and the data rendering was slow.

Second, you can use double buffering technology to solve this problem. So what is double buffering?

Baidu introduction: When we watch TV, the screen we see is called the OSD layer. That is to say, we can only see the image when it is displayed on the OSD layer. Now, I need to create a virtual, invisible OSD layer on which drawings can be drawn (such as drawing points and lines), which I call offscreen (back buffer). This offscreen exists in memory. We draw pictures on it. Things on this offscreen can be displayed on the OSD layer. We need a function to create this offscreen and return the handle (integer pointer), width, height, and pointer to the new offscreen of this offscreen . Pointer to the data buffer . This buffer is an offscreen data buffer created outside the function. The size is the height of the offscreen * the width * the size of each pixel data. Flickering is a common problem in graphics programming. Graphics operations that require multiple complex draw operations can cause the rendered image to flicker or have another unacceptable appearance. The use of double buffering solves these problems. Double buffering uses a memory buffer to solve the flickering problem caused by multiple draw operations. When double buffering is enabled, all drawing operations are rendered first to the memory buffer rather than to the on-screen drawing surface. After all drawing operations are completed, the memory buffer is copied directly to its associated drawing surface. Because only one graphics operation is performed on the screen , image flickering caused by complex drawing operations is eliminated.

To sum up: In computers, animation is regarded as a changing image sequence, consisting of dynamic images frame by frame. These images change with time. The latter frame of image is a modification of the previous frame of image. .

In single-buffer animation, graphics are drawn directly in the display buffer. If the next frame of image is displayed, the screen must be erased, so the screen needs to be erased continuously during the production process. This is also the reason for screen flickering (slow form loading). Double buffer animation has two buffers. In addition to the display buffer, there is also a memory buffer. During the production process, the graphics are first drawn in the memory buffer, and then the images in the memory buffer are copied to the memory cache at one time. area, the display buffer is just an image of the memory buffer.

Example:

Projector and whiteboard. But buffer animation: When using the whiteboard, if it is full and I need to write a page, then I can only erase the content on the whiteboard and then write the content. Double buffer animation: When using the projector, the content needs to be changed. I just need to modify the content in the projector to avoid erasing operations.

3. Specific methods (code display)

1. Solution to stuck C# winform form

Which form needs to be modified to cause page lag? Add this method

 protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;

                cp.ExStyle |= 0x02000000;

                return cp;
            }
           
        }

This method can solve the problem of slow loading of all controls on the page.

In the above code, the CreateParams class is used, which specifically solves the problems of lagging and splashing screens. Consult the information: CreateParams, a property of forms and controls, through which you can easily control some features of a form or control such as borders, maximize, minimize, close, hide buttons, modalize forms, pop-up modes, etc.

2. Then you can also use the following method to solve the problem of slow and unsmooth loading of data in dataGridView. The root cause is also the use of double buffering technology.

You need to write the following code in the corresponding constructor of the form to control the delay in loading data in DataGridView.

this.SetStyle(ControlStyles.OptimizedDoubleBuffer //双缓冲
                | ControlStyles.ResizeRedraw 
               | ControlStyles.AllPaintingInWmPaint, //不擦除背景,减少闪烁
                true);
            this.UpdateStyles();

            //利用反射设置DataGridView的双缓冲
            Type dgvType = this.dgvwCustomerManagement.GetType();
            PropertyInfo pi = dgvType.GetProperty("DoubleBuffered",
               BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(this.dgvwCustomerManagement, true, null);
          

4. Summary

Double buffering technology is an optimization technology for graphical interface drawing that uses two buffers to reduce image flickering and improve drawing efficiency. The following is a summary of double buffering technology:

1. Principle: Double buffering technology uses two buffers, one for drawing images and one for displaying images. The image is drawn in the back buffer, and after completion, the entire image is drawn to the front buffer at once, thus avoiding image flickering.

2. Advantages: Using double buffering technology can improve the drawing efficiency and user experience of the graphical interface. Because the drawing operation occurs in the background, the user can see a complete image, rather than the drawing process element by element. This reduces image flicker and improves user perception of the interface.

3. Implementation method: Various programming languages ​​and graphics libraries can be used to implement double buffering technology. A common approach is to use a double-buffered drawing object, direct the drawing operation to the back buffer, and then draw the entire back buffer to the front buffer.

4. Application scenarios: Double buffering technology is suitable for scenarios that require frequent drawing of graphical interfaces, such as graphics editors, game interfaces, etc. In these scenarios, using double buffering can improve drawing efficiency and user experience, and reduce image flickering.

5. Note: When using double buffering technology, you need to pay attention to memory consumption and performance overhead. Since two buffers are used, the memory footprint may increase. At the same time, since each drawing requires drawing the entire back buffer to the front buffer, drawing time and CPU overhead may increase.

In short, double buffering technology is an optimization technology for graphical interface drawing that uses two buffers to reduce image flickering and improve drawing efficiency. Using double buffering technology can improve user experience, especially in scenarios that require frequent drawing of graphical interfaces. However, there are memory consumption and performance overheads to be aware of when using double buffering technology.

Guess you like

Origin blog.csdn.net/weixin_45309155/article/details/129503221