Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 线程化word例程以将文档转换为PDF_C#_Multithreading - Fatal编程技术网

C# 线程化word例程以将文档转换为PDF

C# 线程化word例程以将文档转换为PDF,c#,multithreading,C#,Multithreading,我是线程新手,已经启动了一个2线程应用程序,它运行一个基本的文档到PDF转换方法。客户端有Word 2003文件要转换 然而,代码运行时,它似乎打开了一个word实例到屏幕,它确实打开了一个框和进度条,然后我线程化了它 我现在应该用另一种方式来处理这个词吗 我试图通过运行多个线程来实现处理器的utlise,并加快转换30000个文档文件的速度 我不希望使用任何第三方工具,只是从网站上的帖子word是最好的转换 主要 Thread1代码 Word.Application word = n

我是线程新手,已经启动了一个2线程应用程序,它运行一个基本的文档到PDF转换方法。客户端有Word 2003文件要转换

然而,代码运行时,它似乎打开了一个word实例到屏幕,它确实打开了一个框和进度条,然后我线程化了它

我现在应该用另一种方式来处理这个词吗

我试图通过运行多个线程来实现处理器的utlise,并加快转换30000个文档文件的速度

我不希望使用任何第三方工具,只是从网站上的帖子word是最好的转换

主要

Thread1代码

     Word.Application word = new Microsoft.Office.Interop.Word.Application();

     // C# doesn't have optional arguments so we'll need a dummy value
     object oMissing = System.Reflection.Missing.Value;

     // Get list of Word files in specified directory
     DirectoryInfo dirInfo = new DirectoryInfo(@"C:\ConvertToPDF\Docs");
     FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

     Thread thr = Thread.CurrentThread;

     if (thr.Name == "Thread 1")
        {
        var orderedSort = wordFiles.OrderBy(f => f.CreationTime);
        }
     else
        {
        var orderedSort = wordFiles.OrderByDescending(f => f.CreationTime);
        }

     word.Visible = false;
     word.ScreenUpdating = false;

     foreach (FileInfo wordFile in wordFiles)
        {
       // Cast as Object for word Open method
       Object filename = (Object)wordFile.FullName;

      // Use the dummy value as a placeholder for optional arguments
      Document doc = word.Documents.Open(ref filename, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
      doc.Activate();

      object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
      object fileFormat = WdSaveFormat.wdFormatPDF;

      // Save document into PDF Format
      doc.SaveAs(ref outputFileName,
                ref fileFormat, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing);

      object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
         ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
            doc = null;
        }

        // word has to be cast to type _Application so that it will find
        // the correct Quit method.
        ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
        word = null;

不要在服务器上使用Word。你会过得很不愉快的

使用OpenXML SDK:


看看我在回答以下问题时使用的代码:


我不知道你在问什么,但这个词并不是毫无头绪的。多线程处理打开Word的应用程序不会改变这一点。当它打开Word文档时,实际上是打开了Word。在它只是一个保存PDF进度框之前,您仍然在打开Word,您根本看不到窗口。不要那样做。使用多个线程对您毫无帮助,因为单个应用程序将不得不序列化来自线程的请求,并可能最终导致死锁。更糟糕的是,除非您真正了解COM互操作,否则您可以为每个文件生成新的Word实例,直到服务器内存耗尽。如果您实际上不需要Word文件,请使用库直接创建PDF文件。即使你这样做了,你仍然需要为此使用一个库OK我明白你的意思..没有办法打开word assign的实例/将其与线程1一起使用。。(不是关上而是重复使用)。还有一个单独的word实例,并将其分配给线程2。看看我的转换为PDF的解决方案:文件都在本地驱动器上,以加快转换为PDF的速度。OpenXML从Office2007开始,客户端有2003个文档文件,然后使用不同的库。我已经成功地使用了它:。是的,我正在寻找一种使用Word运行几个线程的方法
     Word.Application word = new Microsoft.Office.Interop.Word.Application();

     // C# doesn't have optional arguments so we'll need a dummy value
     object oMissing = System.Reflection.Missing.Value;

     // Get list of Word files in specified directory
     DirectoryInfo dirInfo = new DirectoryInfo(@"C:\ConvertToPDF\Docs");
     FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

     Thread thr = Thread.CurrentThread;

     if (thr.Name == "Thread 1")
        {
        var orderedSort = wordFiles.OrderBy(f => f.CreationTime);
        }
     else
        {
        var orderedSort = wordFiles.OrderByDescending(f => f.CreationTime);
        }

     word.Visible = false;
     word.ScreenUpdating = false;

     foreach (FileInfo wordFile in wordFiles)
        {
       // Cast as Object for word Open method
       Object filename = (Object)wordFile.FullName;

      // Use the dummy value as a placeholder for optional arguments
      Document doc = word.Documents.Open(ref filename, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
        ref oMissing, ref oMissing, ref oMissing, ref oMissing);
      doc.Activate();

      object outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
      object fileFormat = WdSaveFormat.wdFormatPDF;

      // Save document into PDF Format
      doc.SaveAs(ref outputFileName,
                ref fileFormat, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing);

      object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
         ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
            doc = null;
        }

        // word has to be cast to type _Application so that it will find
        // the correct Quit method.
        ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
        word = null;