. net任务报告进度

本文关键字:报告 任务 net | 更新日期: 2023-09-27 18:15:55

我有一个通过web服务上传字节数组的任务。每当达到10%、20%、30%、40%等百分比时,我就会报告进度。为了在数据库中更新,我需要向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;
                        }
                    }
                }
            }
        };

. net任务报告进度

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

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...
}