【问题标题】:Is there a way to call PowerPoint's DoEvents() function from a C# addin?有没有办法从 C# 插件调用 PowerPoint 的 DoEvents() 函数?
【发布时间】:2019-05-23 22:31:43
【问题描述】:

我正在尝试在 C# PPT 插件上添加一个在后台运行的常量循环。由于我不希望它导致 PowerPoint 滞后,我想复制 VBA 提供的 DoEvents 功能

我正在尝试做的事情与此类似:这里的想法是让代码在 PowerPoint 保持正常运行的同时每秒打印一个新行。

此代码起初运行正常,但它并不能解决我试图解决的问题,因为它会在发送任何事件(例如 MouseUp)时冻结 PowerPoint,即使没有任何方法在代码中监听事件。


int seconds = 0;
time = DateTime.Now.Second + 1;

while (true)
{

    if (DateTime.Now.Second >= time)
    {
        Debug.WriteLine(seconds + " seconds have passed since the addin started");
        time = DateTime.Now.Second + 1;
    }

    System.Windows.Forms.Application.DoEvents();
}

我直接在 powerpoint 的 VBA 引擎中测试了该方法,它按预期工作(注意:1/86400 相当于在 Ms Office 日期中增加一秒)


Sub countTime()

    nextTime = Now + (1 / 86400)
    counter = 0

    Do While True
        If Now >= nextTime Then
            counter = counter + 1
            Debug.Print (counter & " seconds have passed since the start")
            nextTime = Now + (1 / 86400)
        End If
        DoEvents
    Loop
End Sub


我无法从 C# 插件访问 PowerPoint 的 DoEvents 方法。有什么方法可以复制吗?

【问题讨论】:

  • 我是否正确理解您在Application.DoEvents(); 行收到错误?它说什么?
  • 是的,对不起,我不够清楚。我收到一条错误消息,说应用程序不包含 DoEvents 的定义,没有扩展方法 DoEvents 接受类型为 Application 的第一个参数可以找到
  • Application 是什么类型,或者 Application 是类型本身?在您的代码中,DoEvents 看起来像一个静态方法。
  • 由于这是一个PPT插件应用程序是一个Microsoft.Office.Interop.PowerPoint.Application类型的对象
  • 也许这对您有帮助:DoEvents 方法位于 Interaction VBA 模块中,而不是位于 Application 中。

标签: c# powerpoint


【解决方案1】:

我最终通过简单地使用不同的线程来运行 while 循环来解决这个问题,因为这不会干扰 PowerPoint 事件并且可以按预期工作。

我相信 DoEvents,因为它是从与 PPT 内部使用的不同库调用的最终导致问题,这解释了为什么代码在 PowerPoint 发送任何事件(例如 MouseDown 或 MouseUp)之前一直有效。

感谢所有帮助我的人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多