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?
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,
User contributions licensed under CC BY-SA 3.0