执行时无法从任务栏恢复窗口

时间:2015-08-03 23:15:46

标签: c# windows vba winapi vsto

我在使用Excel 2013和Windows 8.1的C#VSTO Excel应用程序中发生了一个奇怪的问题。

每当我在代码中运行长时间运行的任务并切换到另一个应用程序的窗口时(例如我最大化Explorer或Chrome over Excel),我无法从任务栏恢复Excel窗口(也不能通过Alt + Tab )。我必须最小化其他应用程序的窗口以使焦点回到Excel。在执行任务时,我甚至无法让Show Desktop工作。当我显示进度表单和我根本没有显示表单时,就会发生这种情况。

这可能会发生什么?

是否有一些win32函数可以在执行任务之前/执行时通过pinvoke调用,以使任务栏功能按预期工作?

在执行时似乎能够恢复窗口的VBA代码:

Private Sub CommandButton1_Click()
   Dim i As Long, rng As Range

   Set rng = Range("A1")

   Application.ScreenUpdating = False
   Application.EnableEvents = False
   Application.Cursor = xlWait
   For i = 1 To 500000
     rng.Value2 = "test"
   Next i

   Application.ScreenUpdating = True
   Application.EnableEvents = True
   Application.Cursor = xlDefault
End Sub

Private Sub CommandButton2_Click()
   Application.ScreenUpdating = False
   Application.EnableEvents = False
   Application.Cursor = xlWait

   Application.Wait (Now + TimeValue("0:00:10"))

   Application.ScreenUpdating = True
   Application.EnableEvents = True
   Application.Cursor = xlDefault
End Sub
使用VSTO在C#中的等效代码,在执行代码时无法恢复窗口:

    public void RibbonButton_RunTest1(Office.IRibbonControl control)
    {
        var app = Globals.ThisAddIn.Application;
        app.ScreenUpdating = false;
        app.EnableEvents = false;
        app.Cursor = Excel.XlMousePointer.xlWait;
        //Run a long action that interacts with the Excel object model
        var worksheet = (Excel.Worksheet) app.ActiveSheet;

        var range = worksheet.Range["A1"];
        for (int i = 0; i < 500000; i++)
            range.Value2 = "test";

        app.ScreenUpdating = true;
        app.EnableEvents = true;
        app.Cursor = Excel.XlMousePointer.xlDefault;
    }

    public void RibbonButton_RunTest2(Office.IRibbonControl control)
    {
        var app = Globals.ThisAddIn.Application;
        app.ScreenUpdating = false;
        app.EnableEvents = false;
        app.Cursor = Excel.XlMousePointer.xlWait;
        Thread.Sleep(10000);
        app.ScreenUpdating = true;
        app.EnableEvents = true;
        app.Cursor = Excel.XlMousePointer.xlDefault;
    }

0 个答案:

没有答案