【发布时间】:2012-11-08 09:42:02
【问题描述】:
我是一个 TPL 新手。不确定这个库是否是要走的路,但我想按顺序运行一组异步任务,但不阻塞 UI,而是按顺序运行并按启动顺序回调 - 有点像运行它们同步。所以只有在任务完成并且有 HttpResponseMessage 时才会有效地回调。然后才开始下一个任务。
所以行为会是
运行长时间运行的任务 1,当它完成回调以便我们可以对 UI 做一些事情时,然后 运行下一个长时间运行的任务,当它完成回调时,我们可以更新 UI。 运行一个较短的任务,当它完成回调时,我们可以更新 UI
我已经看到了一些使用调度程序的示例,但无论如何这是我的代码
private void RunSequentially()
{
var lcts = new LimitedConcurrencyLevelTaskScheduler(1);
var factory = new TaskFactory(lcts);
string username = "user";
string password = "password";
var handler = new HttpClientHandler
{
Credentials = new NetworkCredential(username, password), PreAuthenticate = true
};
var client = new HttpClient(handler);
List<Tuple<string,string,string>> taskTuples = new List<Tuple<string, string, string>>();
taskTuples.Add(new Tuple<string, string, string>("POST", "http://longrunning", XDocument.Load(@"./somefile.xml").ToString()));
taskTuples.Add(new Tuple<string, string, string>("GET", "http://getItem", null));
factory.StartNew(
() =>
{
foreach(var tuple in taskTuples)
{
ProcessSequentially(tuple,ProcessCallback, client);
}
});
}
还有 ProcessSequentially 方法
private async void ProcessSequentially(Tuple<string, string, string> tuple, Action<string> callback, HttpClient client)
{
HttpResponseMessage message = null;
if (tuple.Item1 == "POST")
{
message = await
client.PostAsync(
tuple.Item2,
new StringContent(tuple.Item3));
}
else
{
message = await
client.GetAsync(
tuple.Item2);
}
callback(message.ReasonPhrase);
}
现在假设 post 任务将是长时间运行的任务 - 我需要先启动长时间运行的任务并首先回调,然后在第一个回调后启动短时间运行的 GET,而不必阻塞 UI通过 WaitAll 等。有什么方法可以作为 TPL 或 TPL 数据流中的事务来执行此操作,因为在我运行此操作时,POST 确实首先开始,但是因为 get 是一个快速操作,它首先完成并首先返回并且我希望它们像同步操作一样按顺序发生...
所以为了更改代码 - 这是一个更好的方法吗?
foreach (var tuple in taskTuples)
{
factory.StartNew(
() =>
{
this.ProcessSequentially(tuple,client);
}).ContinueWith(this.FinishSequentialTask);
}
【问题讨论】:
-
再次强调我是新手——从我所读到的——我曾认为 await 关键字在幕后执行了延续..
-
这只是链接任务的另一种方式。
-
foreach (var tuple in taskTuples) { factory.StartNew( () => { this.ProcessSequentially(tuple,client); }).ContinueWith(this.FinishSequentialTask); }
-
以上方法会更好吗?
-
我无法回答
Better,但我觉得它更易读。
标签: .net c#-4.0 task-parallel-library