本文整理汇总了C#中Job.Cancel方法的典型用法代码示例。如果您正苦于以下问题:C# Job.Cancel方法的具体用法?C# Job.Cancel怎么用?C# Job.Cancel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Job
的用法示例。
在下文中一共展示了Job.Cancel方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DownloadItemViewModel
public DownloadItemViewModel(Job job)
{
Job = job;
Job.WhenStatusChanges.Subscribe(status => { OnStatusUpdated(); });
OnStatusUpdated();
// update progress every 300ms
Job.WhenDownloadProgresses
.Sample(TimeSpan.FromMilliseconds(300))
.Where(x => !job.IsFinished)
.Subscribe(progress => {
// on main thread
System.Windows.Application.Current.Dispatcher.Invoke(delegate {
DownloadPercent = (double)progress.BytesReceived / Job.File.Bytes * 100;
DownloadPercentFormatted = $"{Math.Round(DownloadPercent)}%";
});
});
// update download speed every 1.5 seconds
var lastUpdatedProgress = DateTime.Now;
long bytesReceived = 0;
Job.WhenDownloadProgresses
.Sample(TimeSpan.FromMilliseconds(1500))
.Where(x => !job.IsFinished)
.Subscribe(progress => {
// on main thread
System.Windows.Application.Current.Dispatcher.Invoke(delegate {
var timespan = DateTime.Now - lastUpdatedProgress;
var bytespan = progress.BytesReceived - bytesReceived;
if (timespan.TotalMilliseconds > 0) {
var downloadSpeed = 1000 * bytespan / timespan.TotalMilliseconds;
DownloadSpeedFormatted = $"{downloadSpeed.Bytes().ToString("#.0")}/s";
}
bytesReceived = progress.BytesReceived;
lastUpdatedProgress = DateTime.Now;
});
});
// update initial size only once
Job.WhenDownloadProgresses
.Take(1)
.Subscribe(progress => {
// on main thread
System.Windows.Application.Current.Dispatcher.Invoke(delegate {
DownloadSizeFormatted = (progress.TotalBytesToReceive > 0 ? progress.TotalBytesToReceive : Job.File.Bytes).Bytes().ToString("#.0");
});
});
// abort job on command
CancelJob.Subscribe(_ => { Job.Cancel(); });
// retry job
RetryJob.Subscribe(_ => { JobManager.RetryJob(Job); });
// delete job
DeleteJob.Subscribe(_ => { JobManager.DeleteJob(Job); });
// setup icon
SetupFileIcon();
}