Exemple #1
0
        private void Worker(object param)
        {
            var self = (Thread)param;

            if (Interlocked.CompareExchange(ref _worker, self, null) != null)
            {
                // Not us, so abort
                return;
            }

            try {
                WorkerWorker();
            } catch (OperationCanceledException) {
            } catch (ObjectDisposedException ex) {
                Trace.TraceError(ex.ToString());
            } catch (Exception ex) {
                if (ex.IsCriticalException())
                {
                    throw;
                }
                VsTaskExtensions.ReportUnhandledException(ex, SR.ProductName, GetType());
            } finally {
                var oldWorker = Interlocked.CompareExchange(ref _worker, null, self);
                Debug.Assert(oldWorker == self, "Worker was changed while running");
            }
        }
Exemple #2
0
        private void Worker(object threadStarted)
        {
            try {
                SynchronizationContext.SetSynchronizationContext(new AnalysisSynchronizationContext(this));
                _scheduler = TaskScheduler.FromCurrentSynchronizationContext();
            } finally {
                ((AutoResetEvent)threadStarted).Set();
            }

            while (!_cancel.IsCancellationRequested)
            {
                IAnalyzable workItem;

                AnalysisPriority pri;
                lock (_queueLock) {
                    workItem     = GetNextItem(out pri);
                    _isAnalyzing = true;
                }
                var evt = AnalysisStarted;
                if (evt != null)
                {
                    evt(this, EventArgs.Empty);
                }

                if (workItem != null)
                {
                    try {
                        var groupable = workItem as IGroupableAnalysisProjectEntry;
                        if (groupable != null)
                        {
                            bool added = _enqueuedGroups.Add(groupable.AnalysisGroup);
                            if (added)
                            {
                                Enqueue(new GroupAnalysis(groupable.AnalysisGroup, this), pri);
                            }

                            groupable.Analyze(_cancel.Token, true);
                        }
                        else
                        {
                            workItem.Analyze(_cancel.Token);
                        }
                    } catch (Exception ex) {
                        if (ex.IsCriticalException() || System.Diagnostics.Debugger.IsAttached)
                        {
                            throw;
                        }
                        VsTaskExtensions.ReportUnhandledException(ex, SR.ProductName, GetType());
                        _cancel.Cancel();
                    }
                }
                else
                {
                    _isAnalyzing = false;
                    WaitHandle.SignalAndWait(
                        _analyzer.QueueActivityEvent,
                        _workEvent
                        );
                }
            }
            _isAnalyzing = false;
        }