Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

420
Vistas
How do I use WebView2 in a console application
string text = "return 'test';";
var webView = new Microsoft.Web.WebView2.WinForms.WebView2();
webView.EnsureCoreWebView2Async(null).RunSynchronously();
var srun = webView.CoreWebView2.ExecuteScriptAsync(text);

When I run the above code EnsureCoreWebView2Async is getting this exception

"Cannot change thread mode after it is set. (Exception from HRESULT: 0x80010106 (RPC_E_CHANGED_MODE))" i What do I need to do to run this with out a winform dlg in a console or windows service?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

It turns out the key thing is to a[STAThread] to the Main function.

    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Microsoft.Web.WebView2.WinForms.WebView2 webView21 = new Microsoft.Web.WebView2.WinForms.WebView2();

            var ecwTask = webView21.EnsureCoreWebView2Async(null);
            while (ecwTask.IsCompleted == false)
            {
                Application.DoEvents();
            };

            var scriptText = @"var test = function(){ return 'apple';}; test();";
            var srunTask = webView21.ExecuteScriptAsync(scriptText);
            while (srunTask.IsCompleted == false)
            {
                Application.DoEvents();
            };

            Console.WriteLine(srunTask.Result);
}
}

Other items that maybe note worthy,

  • Sometimes you need to set the bitness of the application because use of AnyCPU will cause an infanite wait when creating the webview2 COM object.
  • You may need to set the webview2 source property to foce the instance into existance.
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda