private void DoTaskInternal()
        {
            if (!_taskRunning)
            {
                _taskRunning = true;

                Task.Factory.StartNew(() => {
                    if (Interlocked.Read(ref _closed) == 0)
                    {
                        object result = null;

                        try {
                            result = _taskAction();
                        } catch (Exception ex) {
                            Debug.Fail(
                                String.Format("Background task exception {0}. Inner exception: {1}",
                                              ex.Message,
                                              ex.InnerException != null ? ex.InnerException.Message : "(none)")
                                );
                            result = ex;
                        } finally {
                            UIThread.InvokeAsync(() => UIThreadCompletedCallback(result))
                            .HandleAllExceptions(SR.ProductName)
                            .DoNotWait();
                        }
                    }
                    else if (Interlocked.Read(ref _closed) > 0)
                    {
                        UIThread.InvokeAsync((() => UIThreadCanceledCallback(null)))
                        .HandleAllExceptions(SR.ProductName)
                        .DoNotWait();
                    }
                });
            }
        }
Exemple #2
0
            /// <summary>
            /// Navigates to the object that is represented by the specified route.
            /// </summary>
            /// <param name="route">The route of the object to navigate to.</param>
            /// <param name="data">The data to pass with the navigation request.</param>
            /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
            /// <returns>The <see cref="Task{TResult}"/> that represents the asynchronous operation.</returns>
            public async Task <bool> NavigateAsync(string route, object data, CancellationToken cancellationToken = default)
            {
                if (UIThread.CheckAccess())
                {
                    return(collection.Navigate(route, data));
                }

                return(await UIThread.InvokeAsync(() => collection.Navigate(route, data), DispatcherPriority.Normal, cancellationToken));
            }
Exemple #3
0
        /// <summary>
        /// Raises the <see cref="ICommand.CanExecuteChanged"/> event for the current <see cref="DelegateCommand{T}"/>.
        /// </summary>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
        /// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
        public async Task RaiseChangedAsync(CancellationToken cancellationToken = default)
        {
            if (UIThread.CheckAccess())
            {
                OnCanExecuteChanged();

                return;
            }

            await UIThread.InvokeAsync(OnCanExecuteChanged, DispatcherPriority.Normal, cancellationToken);
        }
Exemple #4
0
        /// <summary>
        /// Raises the <see cref="ICommand.CanExecuteChanged"/> event for each <see cref="IDelegateCommand"/> in the specified sequence.
        /// </summary>
        /// <param name="commands">The sequence on whose <see cref="IDelegateCommand"/> elements to raise the event.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param>
        /// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns>
        public static async Task RaiseChangedAsync(this IEnumerable <ICommand> commands, CancellationToken cancellationToken = default)
        {
            Requires.NotNull(commands, nameof(commands));

            List <IDelegateCommand> list = new List <IDelegateCommand>();

            list.AddRange(commands.OfType <IDelegateCommand>());

            if (list.Count > 0)
            {
                await UIThread.InvokeAsync(() => RaiseChanged(list, cancellationToken), DispatcherPriority.Normal, cancellationToken);
            }
        }
        internal async System.Threading.Tasks.Task ProfileProject(SessionNode session, EnvDTE.Project projectToProfile, bool openReport)
        {
            if (!await UIThread.InvokeTask(() => EnsureProjectUpToDate(projectToProfile)) &&
                await UIThread.InvokeAsync(() => MessageBox.Show(Resources.FailedToBuild, Resources.NodejsToolsForVS, MessageBoxButton.YesNo)) == MessageBoxResult.No)
            {
                return;
            }

            var    interpreterArgs = (string)projectToProfile.Properties.Item("NodeExeArguments").Value;
            var    scriptArgs      = (string)projectToProfile.Properties.Item("ScriptArguments").Value;
            var    startBrowser    = (bool)projectToProfile.Properties.Item("StartWebBrowser").Value;
            string launchUrl       = (string)projectToProfile.Properties.Item("LaunchUrl").Value;

            int?port = (int?)projectToProfile.Properties.Item("NodejsPort").Value;

            string interpreterPath = (string)projectToProfile.Properties.Item("NodeExePath").Value;

            string startupFile = (string)projectToProfile.Properties.Item("StartupFile").Value;

            if (String.IsNullOrEmpty(startupFile))
            {
                MessageBox.Show("Project has no configured startup file, cannot start profiling.", Resources.NodejsToolsForVS);
                return;
            }

            string workingDir = projectToProfile.Properties.Item("WorkingDirectory").Value as string;

            if (String.IsNullOrEmpty(workingDir) || workingDir == ".")
            {
                workingDir = projectToProfile.Properties.Item("ProjectHome").Value as string;
                if (String.IsNullOrEmpty(workingDir))
                {
                    workingDir = Path.GetDirectoryName(projectToProfile.FullName);
                }
            }

            RunProfiler(
                session,
                interpreterPath,
                interpreterArgs,
                startupFile,
                scriptArgs,
                workingDir,
                null,
                openReport,
                launchUrl,
                port,
                startBrowser
                );
        }
Exemple #6
0
        private async Task RefreshAsync()
        {
            var buffers           = new HashSet <ITextBuffer>();
            var bufferToErrorList = new Dictionary <ITextBuffer, List <TaskProviderItem> >();

            if (_errorProvider != null)
            {
                lock (_errorSources) {
                    foreach (var kv in _errorSources)
                    {
                        List <TaskProviderItem> items;
                        buffers.UnionWith(kv.Value);

                        lock (_itemsLock) {
                            if (!_items.TryGetValue(kv.Key, out items))
                            {
                                continue;
                            }

                            // Don't care if functions is empty - we need to
                            // perform the refresh to clear out old squiggles
                            foreach (var item in items)
                            {
                                if (item.IsValid)
                                {
                                    List <TaskProviderItem> itemList;
                                    if (!bufferToErrorList.TryGetValue(item.Snapshot.TextBuffer, out itemList))
                                    {
                                        bufferToErrorList[item.Snapshot.TextBuffer] = itemList = new List <TaskProviderItem>();
                                    }

                                    itemList.Add(item);
                                }
                            }
                        }
                    }
                }
            }

            await UIThread.InvokeAsync(() => {
                if (_errorList != null)
                {
                    try {
                        _errorList.RefreshTasks(_cookie);
                    } catch (InvalidComObjectException) {
                        // DevDiv2 759317 - Watson bug, COM object can go away...
                    }
                }

                if (bufferToErrorList.Any())
                {
                    foreach (var kv in bufferToErrorList)
                    {
                        var tagger = _errorProvider.GetErrorTagger(kv.Key);
                        if (tagger == null)
                        {
                            continue;
                        }

                        if (buffers.Remove(kv.Key))
                        {
                            tagger.RemoveTagSpans(span => span.Span.TextBuffer == kv.Key);
                        }

                        foreach (var taskProviderItem in kv.Value)
                        {
                            taskProviderItem.CreateSquiggleSpan(tagger);
                        }
                    }
                }

                if (_errorProvider != null && buffers.Any())
                {
                    // Clear tags for any remaining buffers.
                    foreach (var buffer in buffers)
                    {
                        var tagger = _errorProvider.GetErrorTagger(buffer);
                        tagger.RemoveTagSpans(span => span.Span.TextBuffer == buffer);
                    }
                }
            });
        }
 internal void ReloadHierarchySafe()
 {
     UIThread.InvokeAsync(ReloadHierarchy)
     .HandleAllExceptions(SR.ProductName)
     .DoNotWait();
 }
 private void ForceUpdateStatusBarWithNpmActivitySafe(string activity)
 {
     UIThread.InvokeAsync(() => ForceUpdateStatusBarWithNpmActivity(activity))
     .HandleAllExceptions(SR.ProductName)
     .DoNotWait();
 }
Exemple #9
0
        async Task Scan()
        {
            var scanId = ++_debugScanCount;

            ReportLine(scanId, "Started");

            try
            {
                using (await _lock.LockAsync())
                {
                    _isScanQueued = false;

                    ReportLine(scanId, "Obtained Lock\n");

                    var stopwatch = Stopwatch.StartNew();

                    var query = CreateQuery();

                    uint chunkIndex        = 0;
                    uint chunkSize         = 50;
                    var  chunkItemsAdded   = new List <MonitoredFolderItem>((int)chunkSize);
                    var  chunkItemsRemoved = new List <MonitoredFolderItem>((int)chunkSize);
                    int  addedCount        = 0;
                    int  removedCount      = 0;

                    int thisIndex     = -1;
                    int existingIndex = -1;
                    var existingItems = _cachedItems.ToList();

                    do
                    {
                        if (_cancellationToken.IsCancellationRequested)
                        {
                            break;
                        }

                        var items = await query.GetItemsAsync(chunkIndex, chunkSize);

                        if (!items.Any())
                        {
                            break;
                        }

                        Report(".");

                        foreach (var item in items)
                        {
                            if (_cancellationToken.IsCancellationRequested)
                            {
                                break;
                            }

                            thisIndex++;
                            var thisItem = new MonitoredFolderItem(item);

                            existingIndex++;
                            if (existingIndex < existingItems.Count)
                            {
                                var existingItem = existingItems[existingIndex];
                                if (existingItem.Equals(thisItem))
                                {
                                    continue;
                                }

                                var comparison = existingItem.CompareTo(thisItem);

                                // Delete all existingItems at this index < thisItem
                                while (comparison < 0)
                                {
                                    existingItems.RemoveAt(existingIndex);
                                    chunkItemsRemoved.Add(existingItem);

                                    if (existingItems.Count == existingIndex)
                                    {
                                        break;
                                    }

                                    existingItem = existingItems[existingIndex];
                                    comparison   = existingItem.CompareTo(thisItem);
                                }

                                if (existingIndex == existingItems.Count)
                                {
                                    existingItems.Add(thisItem);
                                    chunkItemsAdded.Add(thisItem);
                                }
                                else if (comparison > 0)
                                {
                                    existingItems.Insert(existingIndex, thisItem);
                                    chunkItemsAdded.Add(thisItem);
                                }
                            }
                            else
                            {
                                existingItems.Add(thisItem);
                                chunkItemsAdded.Add(thisItem);
                            }
                        }

                        if (_cancellationToken.IsCancellationRequested)
                        {
                            break;
                        }

                        // Raise event
                        if (chunkItemsAdded.Any() || chunkItemsRemoved.Any())
                        {
                            await UIThread.InvokeAsync(() => { Changed?.Invoke(this, new MonitoredFolderChangedArgs(chunkItemsAdded, chunkItemsRemoved)); });

                            addedCount   += chunkItemsAdded.Count;
                            removedCount += chunkItemsRemoved.Count;

                            chunkItemsAdded.Clear();
                            chunkItemsRemoved.Clear();
                        }

                        // Move Next
                        chunkIndex += (uint)items.Count();
                    }while (true);

                    // Any remaining existingItems are removed
                    if (existingIndex < existingItems.Count - 1)
                    {
                        var itemsToRemove = existingItems.Where((_, i) => i > existingIndex).ToList();
                        chunkItemsRemoved.Clear();
                        chunkItemsRemoved.AddRange(itemsToRemove);

                        removedCount += itemsToRemove.Count;

                        var numberToRemove = existingItems.Count - 1 - existingIndex;
                        existingItems.RemoveRange(existingItems.Count - numberToRemove, numberToRemove);

                        await UIThread.InvokeAsync(() => { Changed?.Invoke(this, new MonitoredFolderChangedArgs(null, chunkItemsRemoved)); });
                    }

                    // Result
                    _cachedItems = existingItems;

                    stopwatch.Stop();

                    if (chunkIndex > 0)
                    {
                        ReportLine($"+{addedCount} -{removedCount}");
                    }

                    ReportLine(scanId, $"Finished.  Processed {chunkIndex} items in {stopwatch.ElapsedMilliseconds} ms.");

                    ReportLine("Releasing Lock");
                }
            }
            catch (Exception ex)
            {
                ReportLine(scanId, $"Exception\n{ex}");
            }
        }