3

我的任务是在将当前 powerpoint 保存为 WMV 之后获取电影帧。以下代码作为独立应用程序运行良好,但由于“CreateVideoStatus”始终为“PpMediaTaskStatus.ppMediaTaskStatusQueued”,因此它作为 Powerpoint 加载项运行时陷入了无限循环。我的猜测是因为 SaveAs WMV 操作非常耗时,所以操作系统降低了它的优先级,直到 CPU 被加载项本身释放。有人能帮帮我吗?提前致谢。

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {

        var app = this.Application;
        var pres = app.Presentations;
        var file = pres.Open("F:\\Src\\Culturelization\\animation2.pptx");
        file.SaveAs("F:\\Src\\Culturelization\\SimpleDemo\\simple.wmv", PowerPoint.PpSaveAsFileType.ppSaveAsWMV, Office.MsoTriState.msoTrue);
        Boolean isExporting = true;
        do
        {

            Thread.Sleep(1000);
            if (file.CreateVideoStatus == Microsoft.Office.Interop.PowerPoint.PpMediaTaskStatus.ppMediaTaskStatusFailed && file.CreateVideoStatus == Microsoft.Office.Interop.PowerPoint.PpMediaTaskStatus.ppMediaTaskStatusDone)
            {
                isExporting = false;
            }

        } while (isExporting);                

    }
4

1 回答 1

0
  1. 您可以使用新线程检查“powerpoint 另存为 wmv”状态。
    如:BackgroundWorker,避免你的主程序卡顿。

  2. 在您的循环条件中有一个逻辑错误:

    if ( file.CreateVideoStatus == ppMediaTaskStatusFailed && file.CreateVideoStatus == ppMediaTaskStatusDone )
    

    “CreateVideoStatus”一次只有一个状态。
    使用“||” (或)不是“&&”(和)。

参考代码:

using System.ComponentModel;

namespace AddInName
{
    public partial class ThisAddIn
    {
        private BackgroundWorker checkSave_backgroundWorker = new BackgroundWorker();

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            checkSave_backgroundWorker.DoWork += new DoWorkEventHandler(checkSave_backgroundWorker_DoWork);
        }

        // ...

        private void DoSave()
        {
            Globals.ThisAddIn.Application.ActivePresentation.SaveAs( saveAsWmvFileName, Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType.ppSaveAsWMV, Microsoft.Office.Core.MsoTriState.msoTrue );
            checkSave_backgroundWorker.RunWorkerAsync();
        }


        private void checkSave_backgroundWorker_DoWork( object sender, DoWorkEventArgs e )
        {
            bool isDone = false;
            do
            {
                Thread.Sleep( 1000 );
                if ( Globals.ThisAddIn.Application.ActivePresentation.CreateVideoStatus == Microsoft.Office.Interop.PowerPoint.PpMediaTaskStatus.ppMediaTaskStatusDone
                    || Globals.ThisAddIn.Application.ActivePresentation.CreateVideoStatus == Microsoft.Office.Interop.PowerPoint.PpMediaTaskStatus.ppMediaTaskStatusFailed
                    || Globals.ThisAddIn.Application.ActivePresentation.CreateVideoStatus == Microsoft.Office.Interop.PowerPoint.PpMediaTaskStatus.ppMediaTaskStatusNone )
                {
                    isDone = true;
                }
            } while ( !isDone );

            // Finish!
        }
    }
}
于 2017-10-05T06:32:48.677 回答