How to display a wait popup from a Visual Studio extension?

I am writing an extension that sometimes runs a rather long batch job in the background and should provide reasonable guidance to the user that it really works.

If possible, it would be nice to use a pop-up / ialog download that VS2012 is already using, such as the "prepare solution" dialog. Does anyone know how to instantiate this popup from an extension?

If not, are there any good alternatives? It is preferable to display a status bar and a progress bar.

+4
source share
2 answers

I looked for a way to display the progress dialog myself and finally came across a class called CommonMessagePump that provides the same wait dialog that appears in Visual Studio when operations take a lot of time.

A little cumbersome to use, but it seems to work very well. It does not appear until your operation took two seconds or so, and it also supports cancellation.

Assuming you have a class named Item and that it contains the Name property, and you want to process a list of these elements, this can be done as follows:

 void MyLengthyOperation(IList<Item> items) { CommonMessagePump msgPump = new CommonMessagePump(); msgPump.AllowCancel = true; msgPump.EnableRealProgress = true; msgPump.WaitTitle = "Doing stuff..." msgPump.WaitText = "Please wait while doing stuff."; CancellationTokenSource cts = new CancellationTokenSource(); Task task = Task.Run(() => { for (int i = 0; i < items.Count; i++) { cts.Token.ThrowIfCancellationRequested(); msgPump.CurrentStep = i + 1; msgPump.ProgressText = String.Format("Processing Item {0}/{1}: {2}", i + 1, msgPump.TotalSteps, items[i].Name); // Do lengthy stuff on item... } }, cts.Token); var exitCode = msgPump.ModalWaitForHandles(((IAsyncResult)task).AsyncWaitHandle); if (exitCode == CommonMessagePumpExitCode.UserCanceled || exitCode == CommonMessagePumpExitCode.ApplicationExit) { cts.Cancel(); msgPump = new CommonMessagePump(); msgPump.AllowCancel = false; msgPump.EnableRealProgress = false; // Wait for the async operation to actually cancel. msgPump.ModalWaitForHandles(((IAsyncResult)task).AsyncWaitHandle); } if (!task.IsCanceled) { try { task.Wait(); } catch (AggregateException aex) { MessageBox.Show(aex.InnerException.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } } 
+5
source

Source: https://habr.com/ru/post/1447013/


All Articles