예제 #1
0
        internal static (JoinableTaskContext joinableTaskContext, SynchronizationContext synchronizationContext) CreateJoinableTaskContext()
        {
            Thread mainThread;
            SynchronizationContext synchronizationContext;

            switch (ForegroundThreadDataInfo.CreateDefault(ForegroundThreadDataKind.Unknown))
            {
            case ForegroundThreadDataKind.JoinableTask:
                throw new NotSupportedException($"A {nameof(VisualStudio.Threading.JoinableTaskContext)} already exists, but we have no way to obtain it.");

            case ForegroundThreadDataKind.Wpf:
            case ForegroundThreadDataKind.WinForms:
            case ForegroundThreadDataKind.MonoDevelopGtk:
            case ForegroundThreadDataKind.MonoDevelopXwt:
            case ForegroundThreadDataKind.StaUnitTest:
                // The current thread is the main thread, and provides a suitable synchronization context
                mainThread             = Thread.CurrentThread;
                synchronizationContext = SynchronizationContext.Current;
                break;

            case ForegroundThreadDataKind.ForcedByPackageInitialize:
            case ForegroundThreadDataKind.Unknown:
            default:
                // The current thread is not known to be the main thread; we have no way to know if the
                // synchronization context of the current thread will behave in a manner consistent with main thread
                // synchronization contexts, so we use DenyExecutionSynchronizationContext to track any attempted
                // use of it.
                var denyExecutionSynchronizationContext = new DenyExecutionSynchronizationContext(SynchronizationContext.Current);
                mainThread             = denyExecutionSynchronizationContext.MainThread;
                synchronizationContext = denyExecutionSynchronizationContext;
                break;
            }

            return(new JoinableTaskContext(mainThread, synchronizationContext), synchronizationContext);
        }
        /// <summary>
        /// Creates the default ForegroundThreadData assuming that the current thread is the UI thread.
        /// </summary>
        /// <param name="defaultKind">The ForegroundThreadDataKind to fall back to if a UI thread cannot be found</param>
        /// <returns>default ForegroundThreadData values</returns>
        internal static ForegroundThreadData CreateDefault(ForegroundThreadDataKind defaultKind)
        {
            var kind = ForegroundThreadDataInfo.CreateDefault(defaultKind);

            // None of the work posted to the foregroundTaskScheduler should block pending keyboard/mouse input from the user.
            // So instead of using the default priority which is above user input, we use Background priority which is 1 level
            // below user input.
            var taskScheduler = new SynchronizationContextTaskScheduler(new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher, DispatcherPriority.Background));

            return(new ForegroundThreadData(Thread.CurrentThread, taskScheduler, kind));
        }
예제 #3
0
        internal static void Initialize()
        {
            Runtime.AssertMainThread();

            // Initialize Roslyn foreground thread data.
            ForegroundThreadAffinitizedObject.CurrentForegroundThreadData = new ForegroundThreadData(
                Thread.CurrentThread,
                Runtime.MainTaskScheduler,
                ForegroundThreadDataInfo.CreateDefault(ForegroundThreadDataKind.ForcedByPackageInitialize)
                );
        }
        protected override void Initialize()
        {
            base.Initialize();

            // Assume that we are being initialized on the UI thread at this point, and setup our foreground state
            var kind = ForegroundThreadDataInfo.CreateDefault(ForegroundThreadDataKind.ForcedByPackageInitialize);

            // None of the work posted to the foregroundTaskScheduler should block pending keyboard/mouse input from the user.
            // So instead of using the default priority which is above user input, we use Background priority which is 1 level
            // below user input.
            var taskScheduler = new SynchronizationContextTaskScheduler(new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher, DispatcherPriority.Background));

            ForegroundThreadAffinitizedObject.CurrentForegroundThreadData = new ForegroundThreadData(Thread.CurrentThread, taskScheduler, kind);
            ForegroundObject = new ForegroundThreadAffinitizedObject();
        }
예제 #5
0
        protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress)
        {
            await base.InitializeAsync(cancellationToken, progress).ConfigureAwait(true);

            await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            // Assume that we are being initialized on the UI thread at this point, and setup our foreground state
            var kind = ForegroundThreadDataInfo.CreateDefault(ForegroundThreadDataKind.ForcedByPackageInitialize);

            // None of the work posted to the foregroundTaskScheduler should block pending keyboard/mouse input from the user.
            // So instead of using the default priority which is above user input, we use Background priority which is 1 level
            // below user input.
            var taskScheduler = new SynchronizationContextTaskScheduler(new DispatcherSynchronizationContext(Dispatcher.CurrentDispatcher, DispatcherPriority.Background));

            ForegroundThreadAffinitizedObject.CurrentForegroundThreadData = new ForegroundThreadData(Thread.CurrentThread, taskScheduler, kind);
            ForegroundObject = new ForegroundThreadAffinitizedObject();
        }
예제 #6
0
        internal static void Initialize()
        {
            if (Interlocked.CompareExchange(ref initialized, 1, 0) == 1)
            {
                return;
            }

            // Initialize Roslyn foreground thread data.
            ForegroundThreadAffinitizedObject.CurrentForegroundThreadData = new ForegroundThreadData(
                Runtime.MainThread,
                Runtime.MainTaskScheduler,
                ForegroundThreadDataInfo.CreateDefault(ForegroundThreadDataKind.ForcedByPackageInitialize)
                );

            Logger.SetLogger(AggregateLogger.Create(
                                 new RoslynLogger(),
                                 Logger.GetLogger()
                                 ));
        }
예제 #7
0
        internal static void Initialize()
        {
            if (Interlocked.CompareExchange(ref initialized, 1, 0) == 1)
            {
                return;
            }

            // Maybe we should crash here?
            FatalError.Handler         = exception => LoggingService.LogInternalError("Roslyn fatal exception", exception);
            FatalError.NonFatalHandler = exception => LoggingService.LogInternalError("Roslyn non-fatal exception", exception);

            AttachLoggers();

            // Initialize Roslyn foreground thread data.
            ForegroundThreadAffinitizedObject.CurrentForegroundThreadData = new ForegroundThreadData(
                Runtime.MainThread,
                Runtime.MainTaskScheduler,
                ForegroundThreadDataInfo.CreateDefault(ForegroundThreadDataKind.ForcedByPackageInitialize)
                );
        }
        /// <summary>
        /// Creates the default ForegroundThreadData assuming that the current thread is the UI thread.
        /// </summary>
        /// <param name="defaultKind">The ForegroundThreadDataKind to fall back to if a UI thread cannot be found</param>
        /// <returns>default ForegroundThreadData values</returns>
        internal static ForegroundThreadData CreateDefault(ForegroundThreadDataKind defaultKind)
        {
            var kind = ForegroundThreadDataInfo.CreateDefault(defaultKind);

            return(new ForegroundThreadData(Thread.CurrentThread, new SynchronizationContextTaskScheduler(SynchronizationContext.Current), kind));
        }