sha*_*der 5 visual-studio-sdk visual-studio-2012
我正在编写一个扩展,有时在后台运行一个相当长的批处理作业,并且需要向用户提供其实际工作的合理指示.
如果可能的话,最好使用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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1779 次 |
最近记录: |