Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Problem/Bug]: Cannot change thread mode after it is set. #4361

Closed
shishiraltysys opened this issue Feb 8, 2024 · 2 comments
Closed

[Problem/Bug]: Cannot change thread mode after it is set. #4361

shishiraltysys opened this issue Feb 8, 2024 · 2 comments
Assignees

Comments

@shishiraltysys
Copy link

What happened?

When I am using multiple Window application and I want to open a new form of XYZ application, from ABC application, I am using main and ui thread approach, but when i am trying to access XYZ application form that basically uses WebView2 controls, I get an error when i call below code

try
{
var environment = await CoreWebView2Environment.CreateAsync(null, null);

webMessage.CoreWebView2InitializationCompleted += WebBrowser_CoreWebView2InitializationCompleted;

await webMessage.EnsureCoreWebView2Async(environment);

}

Error detail
************** Exception Text **************
System.Runtime.InteropServices.COMException (0x80010106): Cannot change thread mode after it is set. (0x80010106 (RPC_E_CHANGED_MODE))
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHR(Int32 errorCode)
at Microsoft.Web.WebView2.Core.CoreWebView2Environment.CreateAsync(String browserExecutableFolder, String userDataFolder, CoreWebView2EnvironmentOptions options)
at Microsoft.Web.WebView2.WinForms.WebView2.InitCoreWebView2Async(CoreWebView2Environment environment, CoreWebView2ControllerOptions controllerOptions)
at UTIL.App.AlertItemForm.InitializeAsync() in C:\FDB\DEV\UTIL\src\App\ItemForm.cs:line 383
at System.Threading.Tasks.Task.<>c.b__128_0(Object state)
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

Importance

Blocking. My app's basic functions are not working due to this issue.

Runtime Channel

Stable release (WebView2 Runtime)

Runtime Version

1.0.2277.86

SDK Version

1.0.2277.86

Framework

Winforms

Operating System

Windows 11

OS Version

No response

Repro steps

Run the code and getting above issue

Repros in Edge Browser

No

Regression

No, this never worked

Last working version (if regression)

1.0.2277.86

@shishiraltysys shishiraltysys added the bug Something isn't working label Feb 8, 2024
@shishiraltysys
Copy link
Author

@joshholmes @quietfanatic @oggy22 @yildirimcagri-msft
Please help me

@LiangTheDev
Copy link
Member

WebView2 by design requires running from STA: https://learn.microsoft.com/en-us/microsoft-edge/webview2/concepts/threading-model. Try to see whether there is any code calling CoInitializeEx with COINIT_MULTITHREADED. Try change that to be COINIT_APARTMENTTHREADED.

@LiangTheDev LiangTheDev removed the bug Something isn't working label Feb 8, 2024
@victorhuangwq victorhuangwq closed this as not planned Won't fix, can't repro, duplicate, stale Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants