WebView2Feedback icon indicating copy to clipboard operation
WebView2Feedback copied to clipboard

Control flicks a lot and cpu gets very high due to excessive paint messages - WS_EX_COMPOSITED

Open topozepe opened this issue 4 years ago • 17 comments

Description Windows WM_PAINT messages are consecutively being sent (infinite loop) when the top parent window uses the WS_EX_COMPOSITED extended style. The problem is demonstrated in this post: https://stackoverflow.com/questions/65713336/c-sharp-excessive-repainting-of-webview2-control-after-applying-nativewinapi-sty/66678044#66678044 It has an Windows Forms simple example on how to reproduce.

I've answered it with a solution to avoid the flicker on the controls, but it doesn't avoid the paint messages infinite loop / high cpu; the solution was to set the DoubleBuffered property in all the form child controls.

Additionally and regarding the flicker effect, with the WS_EX_COMPOSITED style set in the top window and with the DoubleBuffered property in the child controls, in the versions that I've tested the WebView2 control from versions 1.0.664.37 and 1.0.705.50 doesn't flick, but from version 1.0.774.44 and more recent prereleases the controls flicks a lot, in all versions, the infinite paint messages and high cpu is always present.

I've no ideas on how to fix this, but I think it should be fixed on WebView2 control as it is the source of the paint messages infinite loop.

Version SDK: 1.0.664.37 and beyond Runtime: 89.0.774.57 Framework: WinForms OS: Win Server 2019 v1809

Repro Steps Use the example (ZIP) from the StackOverflow link above, optionally applying the solution from my answer (from António Cunha) to avoid the flicks on the other form controls. With Spy++ it can be observed that WM_PAINT messages are always being triggered; high cpu can also be observed in Task Manager or in VS Diagnostic Tools pane. I expect that the WebView2 control supports correctly a parent window with the WS_EX_COMPOSITED style.

Let me know if this problem is already known and when it is expected to be fixed. I'm available to help as needed, as I'm with a bit urgency to deliver a feature in an application that will start using the WebView2 control.

Many Thanks, António

AB#32255733

topozepe avatar Mar 19 '21 17:03 topozepe

Thanks for the repro and info Antonio - we did have a similar issue in 1.0.781-prerelease: #878. However, 1.0.774.44 should have the fix for that, so it's possible we missed the scenario when WS_EX_COMPOSITED is on the parent HWND. I've added a bug to our backlog and we'll take a look.

champnic avatar Mar 26 '21 00:03 champnic

Any news on this? I'm experiencing the same problem. The WebView2 control flickers on at least every resize, but maybe on other occasions, as well. You can see the background for an instant and then the content gets repainted. WinForms on 4.6.1 and .NET 5.

obartelt avatar Dec 01 '21 18:12 obartelt

@obartelt we haven't looked into this issue yet. Do you have a repro app or video we could take a look at? Are you seeing very high CPU usage even when not resizing?

champnic avatar Dec 01 '21 18:12 champnic

@champnic thanks for the quick reply! No, no high CPU, not even when resizing (5-6%, but I have 16 cores, so yes, maybe it's 100% CPU on one core, but there are a lot of resizes going on). And no, I don't have a repro app right now, but I can try to make one, as soon as I have narrowed the problem down a little more. I'll get back to you!

obartelt avatar Dec 01 '21 18:12 obartelt

Great, thanks! If you want you could open it as a separate issue, as this one was focused more on the high CPU usage and WM_PAINT messages.

champnic avatar Dec 01 '21 19:12 champnic

hello, I have same issue in my application, any fixes? I have flicker in another controls also but less than in webview2 controller

this is a video for the problem https://user-images.githubusercontent.com/32880800/145788635-484cd701-2940-4ad5-8ce5-9abdac950e12.mp4

@champnic

khaledJanky avatar Dec 13 '21 09:12 khaledJanky

I just added a webview2 to two different forms in my Winforms app, and all the controls now constantly flicker. The CPU usage goes to approximately 25% as soon as the web page is loaded.

When the source URI is not set in the WebView2 control, the cpu usage is about 1%.

seattleice avatar Jan 02 '22 09:01 seattleice

hello, @seattleice, @champnic , @topozepe someone on our team have added this to the project. once I deleted them the webview 2 work fine without any flicker

protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle = cp.ExStyle | 0x2000000;
                    cp.ClassStyle |= 0x8;
                return cp;
            }
        } 

khaledJanky avatar Jan 04 '22 06:01 khaledJanky

We recently tracked a major performance problem to this bug. Our application host WebView2 in WS_EX_COMPOSITED parent, which results in infinite stream of WM_PAINT.

This in turn affected interprocess communication via COM, essentially intruducing a major latency for each call. This was unveiled when at a customer that populated Word-documents via COM. A task that previously took 10 s suddenly took 10 minutes.

Are there any workaound such that we could avoid the stream of WM_PAINT messages flooding our message loop?

pontusn avatar Feb 26 '22 06:02 pontusn

@mqudsi Do you have a sample app that we can use to reproduce this issue?

champnic avatar Mar 09 '22 23:03 champnic

I thought it would be easy to replicate but that's not the case.

Here's what I see:

https://user-images.githubusercontent.com/606923/157578617-d64463c7-b5ae-4737-940c-07d6677448a8.mp4

Here's what I figured out:

If I remove the CSS property video { border-radius: 8px; } the flickering goes away. However, in a new WinUI 3 project with a similarish XAML layout and similarish CSS rules, I cannot get any (or the same) video to flicker even with border-radius set.

mqudsi avatar Mar 10 '22 02:03 mqudsi

@mqudsi Would you mind opening your bug in a new GitHub issue? It looks a bit different than the rest of this thread. It might also be good to see if this repros in the Microsoft Edge browser, as I suspect it's an issue in the web platform, and not specific to WebView2. Thanks!

champnic avatar Mar 10 '22 20:03 champnic

any update on this one? I'm also seeing this with latest nuget version. as soon as the webview2 control is "visible" then it enters a WS_PAINT loop and CPU Load Skyrocket

jonferreira avatar Nov 18 '22 09:11 jonferreira

@jonferreira Do you have a sample app you can share which demonstrates the issue? Can you add some details on how often the WS_PAINT is being called, or the CPU spike?

champnic avatar Nov 18 '22 19:11 champnic

Hello,

We are attempting a switch from CEF to WebView2 and have encountered this precise issue. It was fairly simple to create a small sample that demonstrates abnormal CPU usage by simply launching the browser inside an MDI child form.

Our product also leaks memory when this code is uncommented, but i was unable to reproduce this part outside the product.

This sample contains all the things we use in our product to reduce flickering and when this specific code is uncommented the issue occurs.

protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.ExStyle = 0x02000000; //WS_EX_COMPOSITED                // <-- This line specifically
        return cp;
    }
}

Any help would be appreciated. Thanks Orlando

WebView2.Sample.zip

orlribeiro avatar Apr 17 '24 14:04 orlribeiro

I have just noticed the same problem today.

MDI app with WS_EX_COMPOSITED. Having an MDI child window with a WebView2 (showing a blank html page), seems to cause all other MDI child windows in the app to receive constant WM_PAINT messages. CPU load goes to about 15% instead of 3-5%. Hiding the WebView2 stops the constant WM_PAINT messages in the other windows. The windows don't even need to be overlapped with the WebView2.

Flickering due to repainting is mostly not evident due to double-buffering but an IWebBrowser2 in the same app will flicker constantly if the WebView2 window is visible.

rmd-gh avatar May 29 '24 00:05 rmd-gh

Is there any update/fix to this? I'm experiencing the same issue. I have a WinForms application that is dynamically creating a WebView2 component and populating it via the NavigateToString method. I have checked the ExStyle of the WebView2 in the CreateParams method, and its value is 0x02000000 which makes sense since it is being added to a control that is double buffered. When the WebView2 control is displayed, the application consumes an entire core (approximately 8% of the total CPU utilization on a 12-core machine) and is causing flickering of other controls within the application.

Of interesting note, if I take focus away from the WebView2 control (select a different tab in this application's UI), and then click back to the tab that displays the WebView2 control, the flickering and CPU utilization does NOT happen. The flickering only happens the first time the WebView2 control is displayed. I have tried to programmatically select the tab with the WebView2 and then another tab to emulate a user going through the same workflow, but I cannot get it to prevent the flickering the first time a user clicks on the tab and displays the WebView2 control.

I am using version 1.0.2592.51 of the WebView2 control for winforms.

UPDATE: There was a WebView2 control that had been placed on the form during design time. This control was not used and had no code referring to it. Once it was removed from the form so that the only WebView2 controls were created dynamically, the flickering and CPU utilization went away.

UPDATE 2: The flickering and high CPU utilization is still happening. Is there a way to stop the constant WM_PAINT messages from being sent from this control?

ViperDave avatar Jul 15 '24 22:07 ViperDave