Error "The group or resource is not in the correct state to perform the requested operation" when doing WebView2.TrySuspendAsync

Pablo Glomby 186 Reputation points
2022-04-25T17:41:01.93+00:00

I basically need to pause a video when the WebView2 control loses its visibility.
What I did was this:

    /*********************************************************************/
    private async void SuspendWebView2(WebView2 webView2 )
    {
        bool bRes = await webView2.CoreWebView2.TrySuspendAsync();
    }


    /*********************************************************************/
    private void WebCtrl_VisibleChanged(object sender, EventArgs e)
    {
        WebView2 webView2 = (WebView2)sender;
        if (webView2.Visible == false && webView2.CoreWebView2.IsDocumentPlayingAudio )
        {
             webView2.CoreWebView2.IsMuted = true;
            SuspendWebView2(webView2);
        }
        else if (webView2.Visible == true && webView2.CoreWebView2.IsDocumentPlayingAudio )
        {
            webView2.CoreWebView2.IsMuted = false;
            webView2.CoreWebView2.Resume();
        }
    }

But I get:
System.InvalidOperationException: CoreWebView2 members cannot be accessed after the WebView2 control is disposed. ---> System.Runtime.InteropServices.COMException: The group or resource is not in the correct state to perform the requested operation. (Exception from HRESULT: 0x8007139F)

I tried a lot of things including hiding or showing the control (sometimes it works but it is not the same all the time), setting a timer and doing it few seconds after and so on....

Else, is there a way to pause a video? A generic solution like IsMuted=true for audio... Because I don't want to go inside the HTML since the video renderer may be different.

Thanks.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,827 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2022-04-26T06:34:22.407+00:00

    @Pablo Glomby , based on my test, I also reproduced your problem. Based on my research, it may be an issue for the webview2. I recommend that you could sumbit the issue in the Github.

    A generic solution like IsMuted=true for audio... Because I don't want to go inside the HTML since the video renderer may be different.

    Currently, I could not find other way to stop the video like the IsMuted property. I also find a workaround about the html method you mentioned.

    You could refer to the following code to do it.

    private async void SuspendWebView2(WebView2 webView2)  
            {  
                string pausefunctionString = @"  
        var videos = document.querySelectorAll('video');    
        [].forEach.call(videos, function(video) { video.pause(); });  
        ";  
                await webView2.ExecuteScriptAsync(pausefunctionString);  
            }  
    

    However, as you said, the method may have certain flaws because their video renderer may be different.(based on my test, it works for YouTube video)

    BTW, if you still want to use CoreWebView2.TrySuspendAsync method to solve it, I still recommend that you submit the issue in Github.

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful