public void HangReportNotSuppressedOnLongRunningTaskCancelled() { this.Factory.HangDetectionTimeout = TimeSpan.FromMilliseconds(10); var hangReported = new AsyncManualResetEvent(); this.Context.OnReportHang = (hangDuration, iterations, id) => hangReported.Set(); var cancellationSource = new CancellationTokenSource(); JoinableTask?task = this.Factory.RunAsync( async() => { await Task.Delay(40, cancellationSource.Token); }, JoinableTaskCreationOptions.LongRunning); var taskCollection = new JoinableTaskCollection(this.Factory.Context); taskCollection.Add(task); this.Factory.Run( async() => { using (JoinableTaskCollection.JoinRelease tempJoin = taskCollection.Join()) { cancellationSource.Cancel(); await task.JoinAsync().NoThrowAwaitable(); await hangReported.WaitAsync().WithTimeout(UnexpectedTimeout); } }); }
public void HangReportNotSuppressedOnLongRunningTaskCompleted() { this.Factory.HangDetectionTimeout = TimeSpan.FromMilliseconds(10); var hangReported = new AsyncManualResetEvent(); this.Context.OnReportHang = (hangDuration, iterations, id) => hangReported.Set(); JoinableTask?task = this.Factory.RunAsync( async() => { await Task.Delay(30); }, JoinableTaskCreationOptions.LongRunning); task.Join(); Assert.False(hangReported.IsSet); var taskCollection = new JoinableTaskCollection(this.Factory.Context); taskCollection.Add(task); this.Factory.Run( async() => { using (JoinableTaskCollection.JoinRelease tempJoin = taskCollection.Join()) { await hangReported.WaitAsync().WithTimeout(UnexpectedTimeout); } }); }
private void InitializeJoinableTaskFactory() { var context = new JoinableTaskContext(); this.joinableCollection = context.CreateCollection(); this.asyncPump = context.CreateFactory(this.joinableCollection); }
public ProjectCodeModelFactory( VisualStudioWorkspace visualStudioWorkspace, [Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider, IThreadingContext threadingContext, IForegroundNotificationService notificationService, IAsynchronousOperationListenerProvider listenerProvider) { _visualStudioWorkspace = visualStudioWorkspace; _serviceProvider = serviceProvider; _threadingContext = threadingContext; _deferredCleanupTasks = new JoinableTaskCollection(threadingContext.JoinableTaskContext); _deferredCleanupTasks.DisplayName = nameof(ProjectCodeModelFactory) + "." + nameof(_deferredCleanupTasks); _notificationService = notificationService; _listener = listenerProvider.GetListener(FeatureAttribute.CodeModel); // Queue up notifications we hear about docs changing. that way we don't have to fire events multiple times // for the same documents. Once enough time has passed, take the documents that were changed and run // through them, firing their latest events. _documentsToFireEventsFor = new AsyncBatchingWorkQueue <DocumentId>( TimeSpan.FromMilliseconds(visualStudioWorkspace.Options.GetOption(InternalSolutionCrawlerOptions.AllFilesWorkerBackOffTimeSpanInMS)), ProcessNextDocumentBatchAsync, // We only care about unique doc-ids, so pass in this comparer to collapse streams of changes for a // single document down to one notification. EqualityComparer <DocumentId> .Default, _listener, _cancellationTokenSource.Token); _visualStudioWorkspace.WorkspaceChanged += OnWorkspaceChanged; }
public async Task DrainAsync() { while (true) // Keep draining until we're empty { JoinableTaskCollection joinableTasks = _joinableTaskContextNode.CreateCollection(); joinableTasks.DisplayName = GetType().FullName; var tasks = new List <Task>(); lock (_joinableTasks) { foreach (JoinableTask joinableTask in _joinableTasks) { joinableTasks.Add(joinableTask); tasks.Add(joinableTask.Task); } } if (tasks.Count == 0) { break; } // Let these tasks share the UI thread to avoid deadlocks in // case they need it while its blocked on this method. using (joinableTasks.Join()) { await Task.WhenAll(tasks) .ConfigureAwait(false); } } }
public static IEnumerable <object[]> GetPageAndFeatureTemplatesForBuildAsync(string framework) { JoinableTaskContext context = new JoinableTaskContext(); JoinableTaskCollection tasks = context.CreateCollection(); context.CreateFactory(tasks); IEnumerable <object[]> result = new List <object[]>(); switch (framework) { case "CodeBehind": result = context.Factory.Run(() => BuildCodeBehindFixture.GetPageAndFeatureTemplatesAsync(framework)); break; case "MVVMBasic": result = context.Factory.Run(() => BuildMVVMBasicFixture.GetPageAndFeatureTemplatesAsync(framework)); break; case "MVVMLight": result = context.Factory.Run(() => BuildMVVMLightFixture.GetPageAndFeatureTemplatesAsync(framework)); break; case "CaliburnMicro": result = context.Factory.Run(() => BuildCaliburnMicroFixture.GetPageAndFeatureTemplatesAsync(framework)); break; case "Prism": result = context.Factory.Run(() => BuildPrismFixture.GetPageAndFeatureTemplatesAsync(framework)); break; } return(result); }
public void HangReportNotSuppressedOnLongRunningTaskCompleted() { this.Factory.HangDetectionTimeout = TimeSpan.FromMilliseconds(10); bool hangReported = false; this.Context.OnReportHang = (hangDuration, iterations, id) => hangReported = true; var task = this.Factory.RunAsync( async() => { await Task.Delay(30); }, JoinableTaskCreationOptions.LongRunning); task.Join(); Assert.False(hangReported); var taskCollection = new JoinableTaskCollection(this.Factory.Context); taskCollection.Add(task); this.Factory.Run( async() => { using (var tempJoin = taskCollection.Join()) { await Task.Delay(20); } }); Assert.True(hangReported); }
private static void InitializeJoinableTaskFactory(out JoinableTaskCollection joinableCollection, out JoinableTaskFactory asyncPump) { var context = new JoinableTaskContext(); joinableCollection = context.CreateCollection(); asyncPump = context.CreateFactory(joinableCollection); }
public void HangReportNotSuppressedOnLongRunningTaskCancelled() { this.Factory.HangDetectionTimeout = TimeSpan.FromMilliseconds(10); bool hangReported = false; this.Context.OnReportHang = (hangDuration, iterations, id) => hangReported = true; var cancellationSource = new CancellationTokenSource(); var task = this.Factory.RunAsync( async() => { await Task.Delay(40, cancellationSource.Token); }, JoinableTaskCreationOptions.LongRunning); var taskCollection = new JoinableTaskCollection(this.Factory.Context); taskCollection.Add(task); this.Factory.Run( async() => { using (var tempJoin = taskCollection.Join()) { cancellationSource.Cancel(); await task.JoinAsync().NoThrowAwaitable(); await Task.Delay(40); } }); Assert.True(hangReported); }
public void JoinTillEmpty() { JoinableTaskFactory.Context.Factory.Run(async() => { await JoinableTaskCollection.JoinTillEmptyAsync(); }); }
public SolutionRestoreWorker( IAsyncServiceProvider asyncServiceProvider, Lazy <IVsSolutionManager> solutionManager, Lazy <INuGetLockService> lockService, Lazy <Common.ILogger> logger, Lazy <INuGetErrorList> errorList, Lazy <IOutputConsoleProvider> outputConsoleProvider) { if (asyncServiceProvider == null) { throw new ArgumentNullException(nameof(asyncServiceProvider)); } if (solutionManager == null) { throw new ArgumentNullException(nameof(solutionManager)); } if (lockService == null) { throw new ArgumentNullException(nameof(lockService)); } if (logger == null) { throw new ArgumentNullException(nameof(logger)); } if (errorList == null) { throw new ArgumentNullException(nameof(errorList)); } if (outputConsoleProvider == null) { throw new ArgumentNullException(nameof(outputConsoleProvider)); } _asyncServiceProvider = asyncServiceProvider; _solutionManager = solutionManager; _lockService = lockService; _logger = logger; _errorList = errorList; _outputConsoleProvider = outputConsoleProvider; var joinableTaskContextNode = new JoinableTaskContextNode(ThreadHelper.JoinableTaskContext); _joinableCollection = joinableTaskContextNode.CreateCollection(); JoinableTaskFactory = joinableTaskContextNode.CreateFactory(_joinableCollection); _componentModel = new AsyncLazy <IComponentModel>(async() => { return(await asyncServiceProvider.GetServiceAsync <SComponentModel, IComponentModel>()); }, JoinableTaskFactory); _solutionLoadedEvent = new AsyncManualResetEvent(); _isCompleteEvent = new AsyncManualResetEvent(); Reset(); }
public VisualStudioActiveDocumentTracker( IThreadingContext threadingContext, [Import(typeof(SVsServiceProvider))] IAsyncServiceProvider asyncServiceProvider, IVsEditorAdaptersFactoryService editorAdaptersFactoryService) : base(threadingContext, assertIsForeground: false) { _editorAdaptersFactoryService = editorAdaptersFactoryService; _asyncTasks = new JoinableTaskCollection(threadingContext.JoinableTaskContext); _asyncTasks.Add(ThreadingContext.JoinableTaskFactory.RunAsync(async() => { await ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(); var monitorSelectionService = (IVsMonitorSelection)await asyncServiceProvider.GetServiceAsync(typeof(SVsShellMonitorSelection)).ConfigureAwait(false); if (ErrorHandler.Succeeded(monitorSelectionService.GetCurrentElementValue((uint)VSConstants.VSSELELEMID.SEID_DocumentFrame, out var value))) { if (value is IVsWindowFrame windowFrame) { TrackNewActiveWindowFrame(windowFrame); } } monitorSelectionService.AdviseSelectionEvents(this, out var _); })); }
public ProjectCodeModelFactory(VisualStudioWorkspace visualStudioWorkspace, [Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider, IThreadingContext threadingContext) { _visualStudioWorkspace = visualStudioWorkspace; _serviceProvider = serviceProvider; _threadingContext = threadingContext; _deferredCleanupTasks = new JoinableTaskCollection(threadingContext.JoinableTaskContext); _deferredCleanupTasks.DisplayName = nameof(ProjectCodeModelFactory) + "." + nameof(_deferredCleanupTasks); }
public UnconfiguredProjectTasksService([Import(ExportContractNames.Scopes.UnconfiguredProject)] IProjectAsynchronousTasksService tasksService, IProjectThreadingService threadingService, ILoadedInHostListener loadedInHostListener) { _prioritizedTasks = threadingService.JoinableTaskContext.CreateCollection(); _prioritizedTasks.DisplayName = "PrioritizedProjectLoadedInHostTasks"; _tasksService = tasksService; _threadingService = threadingService; _loadedInHostListener = loadedInHostListener; }
public ThreadingContext(JoinableTaskContext joinableTaskContext) { HasMainThread = joinableTaskContext.MainThread.IsAlive; JoinableTaskContext = joinableTaskContext; JoinableTaskFactory = joinableTaskContext.Factory; ShutdownBlockingTasks = new JoinableTaskCollection(JoinableTaskContext); ShutdownBlockingTaskFactory = JoinableTaskContext.CreateFactory(ShutdownBlockingTasks); }
public async Task StartAsync( RestoreOperationSource operationSource, Lazy <ErrorListTableDataSource> errorListDataSource, JoinableTaskFactory jtf, CancellationTokenSource cts) { Assumes.Present(errorListDataSource); Assumes.Present(jtf); Assumes.Present(cts); _operationSource = operationSource; _errorListDataSource = errorListDataSource; _jtc = jtf.Context.CreateCollection(); _taskFactory = jtf.Context.CreateFactory(_jtc); _externalCts = cts; _externalCts.Token.Register(() => _cancelled = true); #if VS14 _progressFactory = t => WaitDialogProgress.StartAsync(_serviceProvider, _taskFactory, t); #else _progressFactory = t => StatusBarProgress.StartAsync(_serviceProvider, _taskFactory, t); #endif await _taskFactory.RunAsync(async() => { await _taskFactory.SwitchToMainThreadAsync(); OutputVerbosity = GetMSBuildOutputVerbositySetting(); switch (_operationSource) { case RestoreOperationSource.Implicit: // background auto-restore _outputConsole = _outputConsoleProvider.CreatePackageManagerConsole(); break; case RestoreOperationSource.OnBuild: _outputConsole = _outputConsoleProvider.CreateBuildOutputConsole(); _outputConsole.Activate(); break; case RestoreOperationSource.Explicit: _outputConsole = _outputConsoleProvider.CreatePackageManagerConsole(); _outputConsole.Activate(); _outputConsole.Clear(); break; } }); if (_errorListDataSource.IsValueCreated) { // Clear old entries _errorListDataSource.Value.ClearNuGetEntries(); } }
public SolutionRestoreWorker( [Import(typeof(SVsServiceProvider))] IServiceProvider serviceProvider, Lazy <IVsSolutionManager> solutionManager, Lazy <INuGetLockService> lockService, [Import("VisualStudioActivityLogger")] Lazy <Common.ILogger> logger, Lazy <ErrorListTableDataSource> errorListTableDataSource) { if (serviceProvider == null) { throw new ArgumentNullException(nameof(serviceProvider)); } if (solutionManager == null) { throw new ArgumentNullException(nameof(solutionManager)); } if (lockService == null) { throw new ArgumentNullException(nameof(lockService)); } if (logger == null) { throw new ArgumentNullException(nameof(logger)); } if (errorListTableDataSource == null) { throw new ArgumentNullException(nameof(errorListTableDataSource)); } _serviceProvider = serviceProvider; _solutionManager = solutionManager; _lockService = lockService; _logger = logger; _errorListTableDataSource = errorListTableDataSource; var joinableTaskContextNode = new JoinableTaskContextNode(ThreadHelper.JoinableTaskContext); _joinableCollection = joinableTaskContextNode.CreateCollection(); _joinableFactory = joinableTaskContextNode.CreateFactory(_joinableCollection); _componentModel = new AsyncLazy <IComponentModel>(async() => { await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); return(serviceProvider.GetService <SComponentModel, IComponentModel>()); }, _joinableFactory); _solutionLoadedEvent = new AsyncManualResetEvent(); _isCompleteEvent = new AsyncManualResetEvent(); Reset(); }
public static IEnumerable <object[]> GetProjectTemplatesForStyleCopAsync() { JoinableTaskContext context = new JoinableTaskContext(); JoinableTaskCollection tasks = context.CreateCollection(); context.CreateFactory(tasks); var result = context.Factory.Run(() => StyleCopGenerationTestsFixture.GetProjectTemplatesForStyleCopAsync()); return(result); }
public static IEnumerable <object[]> GetPageAndFeatureTemplatesForGenerationAsync(string framework) { JoinableTaskContext context = new JoinableTaskContext(); JoinableTaskCollection tasks = context.CreateCollection(); context.CreateFactory(tasks); var result = context.Factory.Run(() => GenerationFixture.GetPageAndFeatureTemplatesAsync(framework)); return(result); }
public JoinableTaskHelper(JoinableTaskContext context) { if (context == null) { throw new ArgumentNullException(nameof(context)); } this.Context = context; this.Collection = context.CreateCollection(); this.Factory = context.CreateFactory(this.Collection); }
public void DisplayName() { var jtc = new JoinableTaskCollection(this.context); Assert.Null(jtc.DisplayName); jtc.DisplayName = string.Empty; Assert.Equal(string.Empty, jtc.DisplayName); jtc.DisplayName = null; Assert.Null(jtc.DisplayName); jtc.DisplayName = "My Name"; Assert.Equal("My Name", jtc.DisplayName); }
static SafeThreading() { try { JoinableTaskFactory = ThreadHelper.JoinableTaskFactory; } catch (NullReferenceException) { JoinableTaskContext context = new JoinableTaskContext(System.Threading.Thread.CurrentThread); JoinableTaskCollection collection = context.CreateCollection(); JoinableTaskFactory = context.CreateFactory(collection); } }
protected JoinableTaskTestBase(ITestOutputHelper logger) : base(logger) { this.dispatcherContext = SingleThreadedTestSynchronizationContext.New(); SynchronizationContext.SetSynchronizationContext(this.dispatcherContext); this.context = this.CreateJoinableTaskContext(); this.joinableCollection = this.context.CreateCollection(); this.asyncPump = this.context.CreateFactory(this.joinableCollection); this.originalThreadManagedId = Environment.CurrentManagedThreadId; this.testFrame = SingleThreadedTestSynchronizationContext.NewFrame(); // Suppress the assert dialog that appears and causes test runs to hang. Trace.Listeners.OfType <DefaultTraceListener>().Single().AssertUiEnabled = false; }
internal static void CompleteSynchronously(this JoinableTaskFactory factory, JoinableTaskCollection collection, Task task) { Requires.NotNull(factory, nameof(factory)); Requires.NotNull(collection, nameof(collection)); Requires.NotNull(task, nameof(task)); factory.Run(async delegate { using (collection.Join()) { await task; } }); }
public async Task StartAsync( RestoreOperationSource operationSource, Lazy <INuGetErrorList> errorList, JoinableTaskFactory jtf, CancellationTokenSource cts) { Assumes.Present(errorList); Assumes.Present(jtf); Assumes.Present(cts); _operationSource = operationSource; _errorList = errorList; _jtc = jtf.Context.CreateCollection(); _taskFactory = jtf.Context.CreateFactory(_jtc); _externalCts = cts; _externalCts.Token.Register(() => _cancelled = true); _progressFactory = t => StatusBarProgress.StartAsync(_asyncServiceProvider, _taskFactory, t); await _taskFactory.RunAsync(async() => { OutputVerbosity = await GetMSBuildOutputVerbositySettingAsync(); switch (_operationSource) { case RestoreOperationSource.Implicit: // background auto-restore _outputConsole = await _outputConsoleProvider.Value.CreatePackageManagerConsoleAsync(); break; case RestoreOperationSource.OnBuild: _outputConsole = await _outputConsoleProvider.Value.CreateBuildOutputConsoleAsync(); await _outputConsole.ActivateAsync(); break; case RestoreOperationSource.Explicit: _outputConsole = await _outputConsoleProvider.Value.CreatePackageManagerConsoleAsync(); await _outputConsole.ActivateAsync(); await _outputConsole.ClearAsync(); break; } }); if (_errorList.IsValueCreated) { // Clear old entries _errorList.Value.ClearNuGetEntries(); } }
static SafeThreading() { try { JoinableTaskFactory = ThreadHelper.JoinableTaskFactory; } catch (NullReferenceException) { #pragma warning disable VSSDK005 // Avoid instantiating JoinableTaskContext JoinableTaskContext context = new JoinableTaskContext(System.Threading.Thread.CurrentThread); #pragma warning restore VSSDK005 // Avoid instantiating JoinableTaskContext JoinableTaskCollection collection = context.CreateCollection(); JoinableTaskFactory = context.CreateFactory(collection); } }
public virtual Task InitializeAsync() { if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA) { JoinableTaskContext = new JoinableTaskContext(); } else { _denyExecutionSynchronizationContext = new DenyExecutionSynchronizationContext(SynchronizationContext.Current !); JoinableTaskContext = new JoinableTaskContext(_denyExecutionSynchronizationContext.MainThread, _denyExecutionSynchronizationContext); } _joinableTaskCollection = JoinableTaskContext.CreateCollection(); JoinableTaskFactory = JoinableTaskContext.CreateFactory(_joinableTaskCollection); return(Task.CompletedTask); }
public void AddTwiceRemoveOnceRemovesWhenNotRefCounting() { var finishTaskEvent = new AsyncManualResetEvent(); var task = this.JoinableFactory.RunAsync(async delegate { await finishTaskEvent; }); var collection = new JoinableTaskCollection(this.context, refCountAddedJobs: false); collection.Add(task); Assert.True(collection.Contains(task)); collection.Add(task); Assert.True(collection.Contains(task)); collection.Remove(task); Assert.False(collection.Contains(task)); finishTaskEvent.Set(); }
public void AddTwiceRemoveOnceRemovesCompletedTaskWhenRefCounting() { var finishTaskEvent = new AsyncManualResetEvent(); var task = this.JoinableFactory.RunAsync(async delegate { await finishTaskEvent; }); var collection = new JoinableTaskCollection(this.context, refCountAddedJobs: true); collection.Add(task); Assert.True(collection.Contains(task)); collection.Add(task); Assert.True(collection.Contains(task)); finishTaskEvent.Set(); task.Join(); collection.Remove(task); // technically the JoinableTask is probably gone from the collection by now anyway. Assert.False(collection.Contains(task)); }
/// <inheritdoc /> public async Task InitializeAsync() { // string name = MyCompanyEventSource.GetName(typeof(MyCompanyEventSource)); // IEnumerable<EventSource> eventSources = MyCompanyEventSource.GetSources(); var mEvent = new ManualResetEvent(false); var startSecondaryThread = RoslynCodeControl.StartSecondaryThread(mEvent, (d) => { }); await mEvent.ToTask(); _d = Dispatcher.FromThread(startSecondaryThread); var joinableTaskContext = new JoinableTaskContext(RoslynCodeControl.SecondaryThread, new DispatcherSynchronizationContext(_d)); _coll = joinableTaskContext.CreateCollection(); var jtf2 = joinableTaskContext.CreateFactory(_coll); JTF2 = jtf2; }