Closed AlonAm closed 11 months ago
@StevenRasmussen @madskristensen hi, please advice, I'm not sure how to resolve the issue above. Either adding 'await JoinableTaskFactory.SwitchToMainThreadAsync();' somewhere or changing the toolkit design so the commands are always added on the main UI thread. Kindly help
Adding commands have to happen on the UI thread as the exception points out. You can switch to the UI thread as you stated, and that should work. For your specific case, I'll let @StevenRasmussen comment if there's anything of interest for the DI system here
As a temporary solution to avoid the error message, is it better to add await JoinableTaskFactory.SwitchToMainThreadAsync();
before the await base.InitializeAsync(cancellationToken, progress);
?
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await JoinableTaskFactory.SwitchToMainThreadAsync();;
await base.InitializeAsync(cancellationToken, progress);
await this.RegisterCommandsAsync();
this.RegisterToolWindows();
// other initializations
}
Sorry for the delay in responding. Just approved and merged the PR. Thanks!
Thank you for your help! 🙂
Version
17.0.13
Description
Error ".ctor must be called on the UI thread" occurs when the package loads in the background.
Analysis
The base package class DIToolkitPackage creates instances of [CommandWrapper<>]() during InitializeAsync. The class CommandWrapper calls IMenuCommandService.AddCommand in the constructor, which requires the main UI thread.
ActivityLog.xml
SetSite failed for package [DatadogPackage]Source: 'Microsoft.VisualStudio.Shell.Framework' Description: .ctor must be called on the UI thread. System.Runtime.InteropServices.COMException (0x8001010E): .ctor must be called on the UI thread. at Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread(String callerMemberName) at Community.VisualStudio.Toolkit.DependencyInjection.Core.CommandWrapper
1..ctor(IServiceProvider serviceProvider, AsyncPackage package)
--- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor
2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
 at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
 at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(Type serviceType)
 at System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey key, Func2 valueFactory)
 at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
 at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
 at Community.VisualStudio.Toolkit.DependencyInjection.DIToolkitPackage
1.<InitializeAsync>d0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Datadog.VisualStudio.Extension.VS2022.DatadogPackage.<InitializeAsync>d24.MoveNext() in D:\a\1\s\src\Datadog.VisualStudio.Extension.VS2022\DatadogPackage.cs:line 41 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.VisualStudio.Shell.AsyncPackage.<>c__DisplayClass21_0.<<Microsoft-VisualStudio-Shell-Interop-IAsyncLoadablePackageInitialize-Initialize>b__1>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Microsoft.VisualStudio.Services.VsTask.RethrowException(AggregateException e) at Microsoft.VisualStudio.Services.VsTask.InternalGetResult(Boolean ignoreUIThreadCheck)