/// <summary>
        /// Performs the asynchronous initialization for the package in cases where IDE supports AsyncPackage.
        ///
        /// This method is always called from background thread initially.
        /// </summary>
        /// <param name="asyncServiceProvider">Async service provider instance to query services asynchronously</param>
        /// <param name="pProfferService">Async service proffer instance</param>
        /// <param name="IAsyncProgressCallback">Progress callback instance</param>
        /// <returns></returns>
        public IVsTask Initialize(Microsoft.VisualStudio.Shell.Interop.COMAsyncServiceProvider.IAsyncServiceProvider asyncServiceProvider, IProfferAsyncService pProfferService, IAsyncProgressCallback pProgressCallback)
        {
            if (!isAsyncLoadSupported)
            {
                throw new InvalidOperationException("Async Initialize method should not be called when async load is not supported.");
            }

            return(ThreadHelper.JoinableTaskFactory.RunAsync <object>(async() =>
            {
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                IVsUIShell shellService = await asyncServiceProvider.GetServiceAsync <IVsUIShell>(typeof(SVsUIShell));
                this.MainThreadInitialization(shellService, isAsyncPath: true);
                return null;
            }).AsVsTask());
        }
示例#2
0
        /// <summary>
        /// Helper method to use async/await with IAsyncServiceProvider implementation
        /// </summary>
        /// <param name="asyncServiceProvider">IAsyncServciceProvider instance</param>
        /// <param name="serviceType">Type of the Visual Studio service requested</param>
        /// <returns>Service object as type of T</returns>
        public static async Task <T> GetServiceAsync <T>(this Microsoft.VisualStudio.Shell.Interop.COMAsyncServiceProvider.IAsyncServiceProvider asyncServiceProvider, Type serviceType) where T : class
        {
            T returnValue = null;

            await ThreadHelper.JoinableTaskFactory.RunAsync(async() =>
            {
                object serviceInstance = null;
                Guid serviceTypeGuid   = serviceType.GUID;
                serviceInstance        = await asyncServiceProvider.QueryServiceAsync(ref serviceTypeGuid);

                // We have to make sure we are on main UI thread before trying to cast as underlying implementation
                // can be an STA COM object and doing a cast would require calling QueryInterface/AddRef marshaling
                // to main thread via COM.
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                returnValue = serviceInstance as T;
            });

            return(returnValue);
        }
        private async Task <IVsContainedLanguageFactory?> GetContainedLanguageFactoryAsync()
        {
            Guid languageServiceId = await GetLanguageServiceIdAsync();

            if (languageServiceId == Guid.Empty)
            {
                return(null);
            }

            IOleAsyncServiceProvider serviceProvider = await _serviceProvider.GetValueAsync();

            object?service = await serviceProvider.QueryServiceAsync(languageServiceId);

            // NOTE: While this type is implemented in Roslyn, we force the cast on
            // the UI thread because they are free to change this to an STA object
            // which would result in an RPC call from a background thread.
            await _projectVsServices.ThreadingService.SwitchToUIThread();

            return(service as IVsContainedLanguageFactory);
        }