示例#1
0
        private async Task <T> GetServiceAsync <T>(IAsyncServiceProvider asyncServiceProvider, Type serviceType) where T : class
        {
            T result = null;
            await ThreadHelper.JoinableTaskFactory.RunAsync(async() =>
            {
                Guid serviceTypeGuid   = serviceType.GUID;
                object serviceInstance = await asyncServiceProvider.QueryServiceAsync(ref serviceTypeGuid);
                await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
                result = serviceInstance as T ?? GetService(serviceType) as T;
            });

            return(result);
        }
示例#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 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);
        }