多次调用后台工作线程?

发布于 2024-12-02 22:35:35 字数 4170 浏览 2 评论 0原文

我正在编写一个 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();
   }

I'm writing a Outlook plugin that transfers large files off to a web service to be copied, but when I have multiple attachments the loop is only sending one of them off to the web service. I can't seem to figure out what I exactly need to do to pass the code to multiple backgroundworkers without a hard limit of attachments. Any ideas?

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();
   }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事未完 2024-12-09 22:35:35

看一下任务类,您可以用它代替后台工作者。您可以将一个附件的复制操作定义为一项任务,并根据附件的数量同时生成多个任务。

Take a look at the Task class, you can use it instead of the background worker. You can define a copy operation for one attachment as a task and depending on the number of attachments you would spawn several tasks simultaneously.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文