English 中文(简体)
吸引背景工作者的经历已过多次?
原标题:Calling a background worker thread multiple times?

我撰写了一份《展望》图,将大宗卷宗转移到一个网络服务处,以供复制,但是,如果我有多个附件,则该书只是寄给网络服务。 我看不出,我完全需要做些什么,才能将守则传给多个背景工作者,而不必严格限制附加。 任何想法?

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();
   }
最佳回答

http://msdn.microsoft.com/en-us/library/system.threading.tasks.task.aspx”rel=“nofollow” 工作班,可以使用,而不是背景工人。 您可以确定一个附件的复印件操作是一项任务,视你将同时完成几项任务。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签