private async void Timer_Tick(object state) // Yeah I know.
        {
            try
            {
                _foregroundDispatcher.AssertBackgroundThread();

                // Timer is stopped.
                _timer.Change(Timeout.Infinite, Timeout.Infinite);

                OnStartingBackgroundWork();

                KeyValuePair <DocumentKey, DocumentSnapshot>[] work;
                lock (_work)
                {
                    work = _work.ToArray();
                    _work.Clear();
                }

                OnBackgroundCapturedWorkload();

                for (var i = 0; i < work.Length; i++)
                {
                    var document = work[i].Value;
                    try
                    {
                        await ProcessDocument(document);
                    }
                    catch (Exception ex)
                    {
                        ReportError(document, ex);
                    }
                }

                OnCompletingBackgroundWork();

                lock (_work)
                {
                    // Resetting the timer allows another batch of work to start.
                    _timer.Dispose();
                    _timer = null;

                    // If more work came in while we were running start the worker again.
                    if (_work.Count > 0)
                    {
                        StartWorker();
                    }
                }

                OnCompletedBackgroundWork();
            }
            catch (Exception ex)
            {
                // This is something totally unexpected, let's just send it over to the workspace.
                await Task.Factory.StartNew(
                    () => _projectManager.ReportError(ex),
                    CancellationToken.None,
                    TaskCreationOptions.None,
                    _foregroundDispatcher.ForegroundScheduler);
            }
        }
Exemplo n.º 2
0
        private async Task ProjectSnapshotManager_ChangedAsync(ProjectChangeEventArgs args, CancellationToken cancellationToken)
        {
            try
            {
                // Don't do any work if the solution is closing
                if (args.SolutionIsClosing)
                {
                    return;
                }

                _projectSnapshotManagerDispatcher.AssertDispatcherThread();

                var projectSnapshot = args.Newer;
                if (projectSnapshot?.ProjectWorkspaceState != null && !_hasNotified)
                {
                    // Un-register this method, we only need to send this once.
                    _projectManager !.Changed -= ProjectSnapshotManager_Changed;
                    var response = await _clientNotifierService.SendRequestAsync(LanguageServerConstants.RazorServerReadyEndpoint);

                    await response.ReturningVoid(cancellationToken);

                    _hasNotified = true;
                }
            }
            catch (Exception ex)
            {
                _projectManager?.ReportError(ex);
            }
        }
 private void ReportError(Exception ex)
 {
     GC.KeepAlive(Task.Factory.StartNew(
                      () => _projectManager.ReportError(ex),
                      CancellationToken.None,
                      TaskCreationOptions.None,
                      _foregroundDispatcher.ForegroundScheduler));
 }
        public async Task DisposeAsync()
        {
            _hostProjectManagerProxy.Changed -= HostProxyProjectManager_Changed;

            await _joinableTaskFactory.SwitchToMainThreadAsync();

            var projects = _projectSnapshotManager.Projects.ToArray();

            foreach (var project in projects)
            {
                try
                {
                    _projectSnapshotManager.ProjectRemoved(((DefaultProjectSnapshot)project).HostProject);
                }
                catch (Exception ex)
                {
                    _projectSnapshotManager.ReportError(ex, project);
                }
            }
        }
        private void ReportError(Exception ex)
        {
            if (_projectManager is null)
            {
                return;
            }

            _ = _projectSnapshotManagerDispatcher.RunOnDispatcherThreadAsync(
                () => _projectManager.ReportError(ex),
                CancellationToken.None);
        }
 private void ReportError(Exception ex)
 {
     _ = _projectSnapshotManagerDispatcher.RunOnDispatcherThreadAsync(
         () => _projectManager.ReportError(ex),
         CancellationToken.None).ConfigureAwait(false);
 }