[英].Net Task reporting progress?
我有一個運行的任務,該任務通過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;
}
}
}
}
};
只是將其作為綁定變量傳遞怎么樣?
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...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.