【问题标题】:VSTO - Disable Excel While Async BackgroundWorker is RunningVSTO - 在异步 BackgroundWorker 运行时禁用 Excel
【发布时间】:2018-05-11 01:34:34
【问题描述】:

我有一个正在为 Excel 开发的 VSTO 加载项。表单允许用户从数据库中查询表,然后进行适当的填充。 Bellow 是我为 BackGround Worker 编写的代码,您可以看到它运行异步并完美地报告进度。

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

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!";


 }

【问题讨论】:

    标签: c# excel vsto


    【解决方案1】:

    请注意,VSTO 中的多线程通常不是一个好主意。但是,我使用进度对话框(I started with this one)取得了成功。您仍然只运行一个与 API 交互的线程,所以据我所知,这没问题。

    你这样称呼它:

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

    您也可以使用WdProtectionType 暂时禁用编辑(这是用于 Word,但我想 Excel 也有类似的东西,可能是 XlProtectionType?),但我不建议这样做。

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2022-07-12
        • 2012-05-30
        • 2021-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多