本文整理汇总了C#中IProgress.ReportWhenNotNull方法的典型用法代码示例。如果您正苦于以下问题:C# IProgress.ReportWhenNotNull方法的具体用法?C# IProgress.ReportWhenNotNull怎么用?C# IProgress.ReportWhenNotNull使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IProgress
的用法示例。
在下文中一共展示了IProgress.ReportWhenNotNull方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: FillDataTo
protected override async Task FillDataTo(System.Net.WebRequest webrqst, CancellationToken cancellationToken, IProgress<ProgressReport> progress)
{
progress.ReportWhenNotNull(() => ProgressReport
.CreateNew(RequestResponseSteps
.Requesting_N_Uploading, 0, 1));
var qd = System.Text.Encoding.UTF8.GetBytes(JsonClientHelper.GetQueryData(RequestData.PostFieldValues));
webrqst.ContentType = "application/x-www-form-urlencoded";
#if SILVERLIGHT_4||SILVERLIGHT_5||DOTNET45
webrqst.ContentLength = qd.Length;
#endif
var rqstStrm = await webrqst.GetRequestStreamAsync().ConfigureAwait(false);
await rqstStrm.WriteAsync(qd, 0, qd.Length).ConfigureAwait(false);
progress.ReportWhenNotNull(() => ProgressReport
.CreateNew(RequestResponseSteps
.Requesting_N_Uploading, 1, 1));
}
示例2: FillDataTo
protected override async Task FillDataTo(System.Net.WebRequest webRequest, CancellationToken cancellationToken, IProgress<ProgressReport> progress)
{
var memStream = new MemoryStream();
// 边界符
var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
// 边界符
var beginBoundary = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
//var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
// 最后的结束符
var endBoundary = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
// 设置属性
webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
// 写入文件
const string filePartHeader =
"Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n";
foreach (var fileKvp in RequestData.UploadFiles)
{
var value = fileKvp.Value;
memStream.Write(beginBoundary, 0, beginBoundary.Length);
var header = string.Format(filePartHeader, fileKvp.Key, value.LocalPath);
var headerbytes = Encoding.UTF8.GetBytes(header);
memStream.Write(headerbytes, 0, headerbytes.Length);
var stream = await value.StreamFactory.ConfigureAwait(false);
await stream.CopyToAsync(memStream).ConfigureAwait(false);
// await memStream.WriteAsync(beginBoundary, 0, beginBoundary.Length);
memStream.Write(CRLFBYTES, 0, CRLFBYTES.Length);
}
if (cancellationToken.IsCancellationRequested) cancellationToken.ThrowIfCancellationRequested();
// 写入字符串的Key
var stringKeyHeader = "Content-Disposition: form-data; name=\"{0}\"" +
"\r\n\r\n{1}\r\n";
foreach (byte[] formitembytes in (
this.RequestData.PostFieldValues
.SelectMany(kvp => kvp.Value.Select(x => new { kvp.Key, Value = x }))
)
.Select(
keyvp =>
{
return string.Format(stringKeyHeader, keyvp.Key, keyvp.Value);
}
)
.Select
(
str => Encoding.UTF8.GetBytes(str)
))
{
memStream.Write(beginBoundary, 0, beginBoundary.Length);
memStream.Write(formitembytes, 0, formitembytes.Length);
}
// 写入最后的结束边界符
memStream.Write(endBoundary, 0, endBoundary.Length);
#if SILVERLIGHT_4||SILVERLIGHT_5||DOTNET45
webRequest.ContentLength = memStream.Length;
#endif
if (cancellationToken.IsCancellationRequested) cancellationToken.ThrowIfCancellationRequested();
var requestStream = await webRequest.GetRequestStreamAsync().ConfigureAwait(false);
memStream.Position = 0;
//await memStream.CopyToAsync(requestStream, 512 * 1024, cancellationToken);
var total = memStream.Length;
var current = 0;
var buffer = new byte[4096];
var sourceStream = memStream;
var eof = false;
while (!eof)
{
progress.ReportWhenNotNull(() => ProgressReport.CreateNew(RequestResponseSteps.Requesting_N_Uploading, current, total));
cancellationToken.ThrowIfCancellationRequested();
var readCount = await sourceStream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
current = current + readCount;
eof = (readCount == 0);
if (!eof) requestStream.Write(buffer, 0, readCount);
}
//.........这里部分代码省略.........