.Net任务报告进度?

时间:2011-11-28 22:22:43

标签: c# c#-4.0 task-parallel-library

我有一个运行的任务,它通过Web服务上传一个字节数组。我想在达到10%,20%,30%,40%等时报告进度。为了更新数据库中的内容,我需要将一个guid传递给Web服务以识别文件报告进度。但我无法从任务内部到达该对象。有什么想法吗?

切入点:

Guid smGuid;
smGuid = Guid.NewGuid();
Task t1 = new Task(action, pubAttFullPath);
t1.Start();
string attFullFilePath = System.IO.Path.GetFullPath(
    mailItem.Attachments[i].FileName);
string attExtension = System.IO.Path.GetExtension(
    mailItem.Attachments[i].FileName);

pubAttFullPath = attFullFilePath;

pubAttFileName = mailItem.Attachments[i].FileName;

任务代码:

 Action<object> action = (object obj) =>
        {
            //Set filename from object
            string FileName;
            FileName = System.IO.Path.GetFileName(obj.ToString());

            //Declare Web Service
            TransferFile.TransferFile ws_TransferFile = new TransferFile.TransferFile();

            //
            bool transfercompleted = false;
            using (FileStream fs = new FileStream(
                 obj.ToString(),
                 FileMode.Open,
                 FileAccess.Read,
                 FileShare.Read))
            {
                //Declare Buffers and Counts
                byte[] buffer = new byte[49152];
                long fileSize = fs.Length;
                long totalReadCount = 0;
                int readCount;
                int currentPacketNumber = 0;
                int percentageComplete = 0;


                //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)
                        {
                                //Copy bytes until buffer is different
                                bytesToTransfer = buffer;
                                ws_TransferFile.WriteBinaryFile(bytesToTransfer, FileName);
                                percentageComplete = (int)(totalReadCount / (double)fileSize * 100);
                            }
                        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, FileName);
                            percentageComplete = 100;                         
                            transfercompleted = true;
                            fs.Close();
                            break;
                        }
                    }
                }
            }
        };

1 个答案:

答案 0 :(得分:3)

如何将其作为绑定变量传递?

Guid smGuid;
smGuid = Guid.NewGuid();
Task t1 = new Task( _ => action( smGuid, pubAttFullPath ), null );
t1.Start();

任务代码:

Action<Guid, string> action = (smGuid, FileName) =>
{
    //Declare Web Service
    TransferFile.TransferFile ws_TransferFile = new TransferFile.TransferFile();

    //
    bool transfercompleted = false;

    // And so on...
}