本文整理汇总了C#中Wrapper.PostEvent方法的典型用法代码示例。如果您正苦于以下问题:C# Wrapper.PostEvent方法的具体用法?C# Wrapper.PostEvent怎么用?C# Wrapper.PostEvent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Wrapper
的用法示例。
在下文中一共展示了Wrapper.PostEvent方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DownloadPackages
/// <summary> 执行下载 </summary>
bool DownloadPackages(Wrapper.RunworkEventArgs rt)
{
System.IO.Directory.CreateDirectory(Context.UpdatePackagePath);
Trace.TraceInformation("开始下载网络更新包");
var workerCount = Math.Max(1, Context.MultipleDownloadCount);
var workers = new List<WebClient>(workerCount);
var evt = new AutoResetEvent(false);
var hasError = false;
//download redirect
if (!string.IsNullOrEmpty(Context.UpdateInfo.PackageUrlTemplate))
{
Trace.TraceInformation("已经重定向下载包地址到 {0}", Context.UpdateInfo.PackageUrlTemplate);
Context.UpdateDownloadUrl = Context.UpdateInfo.PackageUrlTemplate;
}
//Ping
if (!string.IsNullOrEmpty(Context.UpdateInfo.UpdatePingUrl))
{
try
{
Context.CreateWebClient().UploadData(new Uri(Context.UpdateInfo.UpdatePingUrl), new byte[0]);
}
catch (Exception)
{
}
}
//生成下载队列
Trace.TraceInformation("正在初始化 {0} 个WebClient", workerCount);
for (var i = 0; i < workerCount; i++)
{
var clnt = Context.CreateWebClient();
clnt.DownloadFileCompleted += (s, e) =>
{
var pkg = e.UserState as PackageInfo;
var cnt = s as WebClient;
pkg.LastError = e.Error;
if (e.Error != null)
{
Trace.TraceWarning("包【" + pkg.PackageName + "】下载失败:" + e.Error.Message);
rt.PostEvent(PackageDownloadFailed, this, new PackageEventArgs(pkg));
}
else if (pkg.IsLocalFileValid != true)
{
Trace.TraceWarning("包【" + pkg.PackageName + "】MD5校验失败", "错误");
pkg.LastError = new Exception("不文件哈希值不正确或文件不存在");
rt.PostEvent(PackageHashMismatch, this, new PackageEventArgs(pkg));
}
if (pkg.LastError != null)
{
//如果出错,且重试次数在限制范围内,则重试
pkg.IncreaseFailureCounter();
if (pkg.RetryCount <= Context.MaxiumRetryDownloadCount)
{
Trace.TraceWarning("包【" + pkg.PackageName + "】未能成功下载,正在进行第 " + pkg.RetryCount + " 次重试,最大重试次数为 " + Context.MaxiumRetryDownloadCount, "错误");
cnt.DownloadFileAsync(new Uri(pkg.SourceUri), pkg.LocalSavePath, pkg);
rt.PostEvent(PackageDownloadRetried, this, new PackageEventArgs(pkg));
return;
}
//标记出错
hasError = true;
}
//包下载完成事件
pkg.IsDownloading = false;
pkg.IsDownloaded = pkg.LastError == null;
rt.PostEvent(PackageDownloadFinished, this, new PackageEventArgs(e.UserState as PackageInfo));
lock (PackagesToUpdate)
{
Trace.TraceInformation("包【" + pkg.PackageName + "】下载操作完成:" + (pkg.IsDownloaded ? "下载成功" : "下载失败"));
evt.Set();
}
};
clnt.DownloadProgressChanged += (s, e) =>
{
var pkg = e.UserState as PackageInfo;
pkg.DownloadedSize = e.BytesReceived;
pkg.PackageSize = e.TotalBytesToReceive > 0 ? e.TotalBytesToReceive : pkg.PackageSize;
rt.PostEvent(DownloadProgressChanged, this,
new PackageDownloadProgressChangedEventArgs(pkg, pkg.PackageSize,
pkg.DownloadedSize, e.ProgressPercentage));
};
workers.Add(clnt);
}
//开始处理事务
while (!hasError)
{
var breakFlag = false;
lock (PackagesToUpdate)
{
//没有错误,则分配下个任务
WebClient client;
//.........这里部分代码省略.........