【发布时间】:2015-11-09 20:57:04
【问题描述】:
这只是我在窗口服务中所做的事情的想法。 我从这个video 中得到了并行处理的想法。
我有两个不同的方法和一个模型类。
模型类代码:
public class Email(){
public string Recipient { get; set; }
public string Message { get; set; }
}
方法是这样的:
public void LoadData(){
while(Main.IsProcessRunning){
// 1. Get All Emails
var emails = new dummyRepositories().GetAllEmails(); //This will return List<Emails>.
// 2. Send it
// After sending assume that the data will move to other table so it will not be query again for the next loop.
SendDataParallel(emails);//this will function async? even though the calling method is sync.
// This will continue here or wait until it already send?
// If it will continue here even though it will not send already
// So there's a chance to get the email again for the next loop and send it again?
}
}
//This will send email at parallel
public async void SendDataParallel(IList<Email> emails){
var allTasks = emails.Select(SendDataAsync);
await TaskEx.WhenAll(allTasks);
}
//Assume this code will send email asynchronously. (this will not send email, for sample only)
public async void SendDataAsync(Email email){
using (var client = new HttpClient())
{
client.PostAsync(email);
}
}
我只想获取所有排队的电子邮件,然后并行发送,然后等到它已经发送。
我避免在收到的每封电子邮件中使用foreach。
【问题讨论】:
-
问题或问题是什么?
-
@SriramSakthivel 已编辑。,.
-
@janmvtrinidad 这里还是没有问题。
-
@AntP 我把问题作为代码注释。,.
标签: c# asynchronous async-await c#-5.0