2011-09-03 89 views
1

我正在寫一個Outlook插件,用於將大文件傳輸到要複製的Web服務,但是當我有多個附件時,循環僅將其中一個附件發送到Web服務。我似乎無法弄清楚我需要做什麼才能將代碼傳遞給多個背景工作者而沒有附件的硬限制。有任何想法嗎?多次調用後臺工作線程?

private BackgroundWorker bw = new BackgroundWorker(); 

    public string pubAttFullPath = null; 
    public string pubAttFileName = null; 
    public void SM_ItemSend(Object Item, ref bool Cancel) 
    { 
     Outlook.MailItem mailItem = Item as Outlook.MailItem; 
     if (mailItem != null) 
     { 
      int minAttachSize = 40960000; //SM_GetMinSize(); 
      for (int i = 1; i<=mailItem.Attachments.Count; i++) 
      { 
       if (mailItem.Attachments[i].Size < minAttachSize) 
       { 
        System.Windows.Forms.MessageBox.Show("This does NOT meet the minimum attachment size of " + minAttachSize); 
       } 
       else 
       { 
        string attFullFilePath = System.IO.Path.GetFullPath(mailItem.Attachments[i].FileName); 
        pubAttFullPath = attFullFilePath; 
        pubAttFileName = mailItem.Attachments[i].FileName; 

        Guid smGuid; 
        smGuid = Guid.NewGuid(); 

        bw.WorkerReportsProgress = true; 
        bw.WorkerSupportsCancellation = true; 
        bw.DoWork += new DoWorkEventHandler(bw_DoWork); 
        bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); 
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted); 


        if (bw.IsBusy != true) 
        { 
         bw.RunWorkerAsync(); 
        } 

        mailItem.Attachments[i].Delete();  

       } 
      }     
     } 
    } 

private void bw_DoWork(object sender, DoWorkEventArgs e) 
    { 
     System.Windows.Forms.Application.DoEvents(); 
     BackgroundWorker worker = sender as BackgroundWorker; 
     if ((!worker.CancellationPending == true)) 
     { 

     TransferFile.TransferFileSoapClient ws_TransferFile = new TransferFile.TransferFileSoapClient(); 

      bool transfercompleted = false; 
      using (FileStream fs = new FileStream(
       pubAttFullPath, 
       FileMode.Open, 
       FileAccess.Read, 
       FileShare.Read)) 
      { 
       //Declare Buffers and Counts 
       byte[] buffer = new byte[49152]; 
       long fileSize = fs.Length; 
       long totalReadCount = 0; 
       int readCount; 
       //Loop and copy file until it changes to not exactly the same byte count as the buffer 
       //which means the file is about to complete. 
      while ((readCount = 
        fs.Read(buffer, 0, buffer.Length)) > 0) 
       {    
        if (!transfercompleted) 
        { 
         totalReadCount += readCount; 
         byte[] bytesToTransfer; 

         if (readCount == buffer.Length) 
         { 
          // Shortcut to not need to copy more bytes. 
          bytesToTransfer = buffer; 
          ws_TransferFile.WriteBinaryFile(bytesToTransfer, pubAttFileName); 
         } 
         else 
         { 
          // Only a part is requred to upload, 
          // copy that part. 
          List<byte> b = new List<byte>(buffer); 

          bytesToTransfer = 
           b.GetRange(0, readCount).ToArray(); 
          ws_TransferFile.WriteBinaryFile(bytesToTransfer, pubAttFileName); 

          transfercompleted = true; 
          break; 
         }       
        } 
       } 
      } 
     } 
     //Cancel the job, cause for some reason it likes to loop twice and ruin your transfer 
     e.Cancel = true; 
     worker.CancelAsync(); 
    } 

回答

0

看看Task class,你可以用它來代替後臺工作者。您可以將一個附件的複製操作定義爲一項任務,並根據附件數量同時產生多個任務。

+0

所以,我現在試着使用它,並且確實對多個附件有效,但是我原來的問題,以及爲什麼我最初去了BackgroundWorker,當它做這個工作時,它凍結了等待任務完成的outlook。 。有什麼辦法解決這個問題 –

+0

沒關係,我像一個愚蠢的人打電話給Wait()。謝謝你,那就是訣竅! –

+0

儘管與BackgroundWorker進程相比,我在傳輸文件時收到了37億個頁面錯誤,並且速度超過了100倍... –