Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.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# 多次调用后台工作线程?_C#_Multithreading_Wcf_Outlook_Backgroundworker - Fatal编程技术网

C# 多次调用后台工作线程?

C# 多次调用后台工作线程?,c#,multithreading,wcf,outlook,backgroundworker,C#,Multithreading,Wcf,Outlook,Backgroundworker,我正在编写一个Outlook插件,它将大文件传输到web服务进行复制,但是当我有多个附件时,循环只将其中一个发送到web服务。我似乎不知道我到底需要做什么才能将代码传递给多个后台工作人员,而无需严格限制附件。有什么想法吗 private BackgroundWorker bw = new BackgroundWorker(); public string pubAttFullPath = null; public string pubAttFileName = null;

我正在编写一个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();
   }
private BackgroundWorker bw=new BackgroundWorker();
公共字符串pubAttFullPath=null;
公共字符串pubAttFileName=null;
公共无效SM_ItemSend(对象项,参考布尔取消)
{
Outlook.MailItem MailItem=作为Outlook.MailItem的项目;
if(mailItem!=null)
{
int=40960000;//SM_GetMinSize();
对于(int i=1;i 0)
{             
如果(!已完成)
{
totalReadCount+=读取计数;
字节[]ByTestOtTransfer;
if(readCount==buffer.Length)
{
//无需复制更多字节的快捷方式。
ByTestOtTransfer=缓冲区;
ws_TransferFile.WriteBinaryFile(ByTestotTransfer,pubAttFileName);
}
其他的
{
//只需要上传一部分,
//复制那部分。
列表b=新列表(缓冲区);
bytesToTransfer=
b、 GetRange(0,readCount).ToArray();
ws_TransferFile.WriteBinaryFile(ByTestotTransfer,pubAttFileName);
transfercompleted=true;
打破
}                         
}
}
}
}
//取消工作,因为出于某种原因,它喜欢循环两次,破坏你的调动
e、 取消=真;
worker.CancelAsync();
}

查看,您可以使用它而不是后台工作程序。您可以将一个附件的复制操作定义为一个任务,具体取决于同时生成多个任务的附件数量。

因此,我现在尝试使用该操作,但该操作适用于多个附件,但我最初遇到的问题以及我最初为什么去找BackgroundWorker的原因是,当它执行工作时,它冻结outlook,等待任务完成。。。有什么办法吗?没关系,我打电话给Wait()就像个傻瓜。谢谢你,老兄,这就成功了!虽然,与BackgroundWorker进程相比,我在传输文件时遇到了37亿个页面错误,速度慢了100倍多。。。