I would like to know why WebView2.EnsureCoreWebView2Async(null) does not wait for processing using wait.

Asked 2 years ago, Updated 2 years ago, 311 views

The presentation code is the class that draws html using WebView 2 and its initialization process.

webView.EnsureCoreWebView2Async(null); processing is not done in Form1() inside the comment section.

webView.EnsureCoreWebView2Async(null); is not run in the form() function in the InitializeAsync(); function, so I created the Init() function and ran it, but it was not running.

view.Render()Why is it possible that wait is the cause because it is performed when mixed with the drawing process in the function?

I'm waiting for wait.

I tried to create a function based on reference site B, but it has not been executed.

Reference Site A: https://zenn.dev/vatscy/articles/ba2263bdfadfeb805379
Reference Site B: https://stackoverflow.com/questions/66550671/ensurecorewebview2async-not-ready-even-after-corewebview2initializationcompleted

using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using Microsoft.Web.WebView2.Wpf;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using static System.Net.Mime.MediaTypeNames;

namespace WinFormsApp1
{
    public record sample
    (
        string userName,
        US>string test,
        bool IsDelete
    );


    public partial class Form 1:Form
    {
        private HtmlView view;
        /// string text=System.IO.File.ReadAllText(@"debug.js");
        
/////////////////////////////////////////////////////////////////////////////////////
        private async void Init()
        {
            wait view.webView.EnsureCoreWebView2Async(null);
        }
/////////////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////////////
        public Form 1()
        {
            InitializeComponent();

            
            view = new HtmlView (new Size(Size.Width, Size.Height), this);
            // view.InitializeAsync();

            Init();

            view.Render("asset/index.html");
            view.Show();

        }
/////////////////////////////////////////////////////////////////////////////////////
    }
}


using Microsoft.Web.WebView2.WinForms;
using System;
using System.Collections.General;
using System.Text;

namespace WinFormsApp1
{
    public class HtmlView
    {
        public WebView 2 webView;

        public HtmlView (Size size, Form form) 
        {

            webView = new WebView 2();
            webView.Size=size;
            webView.Anchor=AnchorStyles.Left|AnchorStyles.Right|AnchorStyles.Top|AnchorStyles.Bottom;
            webView.BringToFront();

            form.Controls.Add (webView);

            // InitializeAsync();

        }

        public async void InitializeAsync()
        {
            waitwebView.EnsureCoreWebView2Async(null);
        }


        public void Show()
        {
            webView.Show();
        }
/////////////////////////////////////////////////////////////////////////////////////
        public async void render (string path)
        {
            
            StreamReader sr=new StreamReader("asset/index.html", Encoding.GetEncoding("UTF-8"));
            string str = sr.ReadToEnd();

            waitwebView.EnsureCoreWebView2Async(null);

            webView.NavigateToString(str);                    
        }
/////////////////////////////////////////////////////////////////////////////////////
        public async void setJS(string path)
        {
            StreamReader srr=new StreamReader("asset/script/Main.js", Encoding.GetEncoding("UTF-8"));
            strings = srr.ReadToEnd();
            wait webView.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(ss);

        }


        public void JsonStringData(string jsonPath)
        {
            StreamReader str=new StreamReader("asset/test.json", Encoding.GetEncoding("UTF-8"));
            string json = str.ReadToEnd();
            webView.CoreWebView2.PostWebMessageAsJson(json);


        }

    }
}

c#

2022-11-24 00:54

1 Answers

It says async void Init(), but Radian wants to know why Xabe.FFmpeg does not encode video files

Let's not do async void other than event handler

Do you remember being advised that Also, I will answer Same answer

The Form1_Load method is safe.

I would have suggested thatThese are meaningful things.for

publicForm1()
{
    InitializeComponent();

    view = new HtmlView (new Size(Size.Width, Size.Height), this);
    // view.InitializeAsync();

    Init();

    view.Render("asset/index.html");
    view.Show();
}

The and Form1 constructors describe actions, but you have not yet seen the form while the constructor is running.You can see this by reading the caller Program.cs.

static void Main(){
    ApplicationConfiguration.Initialize();
    Application.Run(newForm1());
}    

After the Form1 constructor completes, the Application.Run() runs.UI processing (message loops) works in this context.

I have a question, why do I have to write wait in addition to the form_load function even though there is wait in the following functions?

The reason for this is also explained in Best Practices for Asynchronous Programming in Radian's response.For the time being, always use when calling the async method.(And async void must be avoided as a method definition to allow the caller to wait).


2022-11-24 08:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.