gpt4 book ai didi

visual-studio-2012 - 如何从 Visual Studio 扩展显示等待弹出窗口?

转载 作者:行者123 更新时间:2023-12-04 18:46:24 25 4
gpt4 key购买 nike

我正在编写一个扩展,它有时会在后台运行相当长的批处理作业,并且需要向用户提供一个合理的指示,表明它实际上正在工作。

如果可能,最好使用 VS2012 已经使用的加载弹出窗口/ialog,例如“准备解决方案”对话框。有谁知道如何从扩展程序创建该弹出窗口的实例?

如果没有,有什么好的替代方案吗?能够显示状态字符串和进度条会更可取。

最佳答案

我一直在寻找一种自己显示进度对话框的方法,最后偶然发现了一个名为 CommonMessagePump 的类。当操作需要很长时间才能完成时,它提供了与 Visual Studio 中显示的相同的等待对话框。

使用起来有点麻烦,但看起来效果还不错。它直到您的操作花费两秒钟左右才会显示,并且它还提供取消支持。

假设您有一个名为 Item 的类并且它包含一个名为 Name 的属性并且您想要处理这些项目的列表,可以通过以下方式完成:

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);
}
}
}

关于visual-studio-2012 - 如何从 Visual Studio 扩展显示等待弹出窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13457948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com