VSTO - 在异步后台辅助角色运行时禁用 Excel



我有一个 VSTO 加载项,我正在为 Excel 开发。窗体允许用户从数据库中查询表,然后相应地填充。 Bellow是我的BackGround Worker代码,如您所见,它运行异步并完美地报告进度。

我的问题是,当进程运行时,用户可以在Excel中操作和单击。我知道这是异步函数的重点,但在这种情况下,它会导致后台工作者被中断。有没有办法完全禁用点击或活动工作表?

private void button1_Click(object sender, EventArgs e)
{
//run
//this.backgroundWorker3.RunWorkerAsync();
System.Threading.SynchronizationContext.SetSynchronizationContext(new WindowsFormsSynchronizationContext());
BackgroundWorker bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker3_ProgressChanged);
bw.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker3_DoWork);
bw.RunWorkerAsync();

}
private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = (BackgroundWorker)sender;
worker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker3_WorkerCOmpleted);
if (!_isExtended)
{
oneline ol = new oneline();
ol.buildOneline(this._wheres,worker );
}
else
{
ExtendedOneline ole = new ExtendedOneline();
ole.buildExtendedOneline(this._wheres,worker);
}
//worker.ReportProgress();
e.Result = "COMPLETE!";

}

请注意,VSTO 中的多线程处理通常不是一个好主意。但是,我已经成功地使用进度对话框(我从这个对话框开始(。您仍然只运行一个与 API 交互的线程,因此据我所知没关系。

你这样称呼它:

ProgressDialogResult result = ProgressDialog.Execute(parent, name, () =>
{
// do whatever
}, new ProgressDialogSettings(true, true, false));

您也可以使用WdProtectionType暂时禁用编辑(这是针对Word的,但我想Excel也有类似的东西,也许是XlProtectionType?(,但我不推荐它。

我相信你可以简单地使用:

Globals.ThisAddIn.Application.Cursor = Excel.XlMousePointer.xlWait
// ... your BackgroundWorker Running here
Globals.ThisAddIn.Application.Cursor = Excel.XlMousePointer.xlDefault;

或者也许与:Globals.ThisAddIn.Application.ScreenUpdating

最新更新