我希望异步运行Windows服务中长时间运行的进程 - 每隔3秒轮询一次进程并使用SignalR报告。下面的代码(理论上)将以基于事件的方式执行此操作,但我不想在每次更改时触发进度。
有人可以提供一个简单的示例,说明如何专门实现此过程以启动流程和轮询/报告进度。请记住,我已经完成了几年的全职开发!
public async Task<string> StartTask(int delay)
{
var tokenSource = new CancellationTokenSource();
var progress = new Progress<Tuple<Int32, Int32>>();
progress.ProgressChanged += (s, e ) =>
{
r2hProcessesProxy.Invoke("DownloadNotify", string.Format("Major={0} - Minor={1}", e.Item1 , e.Item2 ));
};
var task = DownloadTask.DoDownload(delay, tokenSource.Token, new Progress<Tuple<Int32,Int32>>(p => new Tuple<Int32, Int32>(0,0)));
await task;
return "Task result";
}
答案 0 :(得分:0)
您可以使用Reactive Extensions(Rx)执行此操作。查看Throttle()
方法:https://msdn.microsoft.com/en-us/library/hh229400(v=vs.103).aspx
using System;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
public class Test
{
public async Task<string> StartTask(int delay)
{
var tokenSource = new CancellationTokenSource();
var progress = new Progress<Tuple<Int32, Int32>>();
var observable = Observable.FromEvent<EventHandler<Tuple<Int32, Int32>>, Tuple<Int32, Int32>>(h => progress.ProgressChanged += h, h => progress.ProgressChanged -= h);
var throttled = observable.Throttle(TimeSpan.FromSeconds(3));
using (throttled.Subscribe(e =>
{
r2hProcessesProxy.Invoke("DownloadNotify", string.Format("Major={0} - Minor={1}", e.Item1, e.Item2));
}))
{
await DownloadTask.DoDownload(delay, tokenSource.Token, new Progress<Tuple<Int32, Int32>>(p => new Tuple<Int32, Int32>(0, 0)));
}
return "result";
}
}
的更多信息