gpt4 book ai didi

c# - 如何将 IEumerable 集合添加到队列并在 .NET 中异步处理每个项目?

转载 作者:可可西里 更新时间:2023-11-01 08:28:53 24 4
gpt4 key购买 nike

我已经创建了一个方法来使用交换服务器读取收件箱新消息,如下所示。如何将这些 IEnumerable 集合添加到队列并异步处理队列中的每个项目列表?

private static IEnumerable<ExchangeEmailInformation> GetInboxItems(ExchangeService service)
{
var emailInformations = new List<ExchangeEmailInformation>();
try
{
SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
var itemview = new ItemView(int.MaxValue);
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, itemview);
Console.WriteLine("\n-------------Result found:-------------");
service.LoadPropertiesForItems(findResults, PropertySet.FirstClassProperties);

foreach (var item in findResults)
{
emailInformations.Add(new ExchangeEmailInformation
{
Attachment = item.Attachments ?? null,
Body = item.Body.BodyType == BodyType.HTML ? ConvertHtml.ToText(item.Body.Text) : item.Body.Text,
Subject = item.Subject,
RecievedDate = item.DateTimeReceived
});
}
}
catch (Exception ee)
{
Console.WriteLine("\n-------------Error occured:-------------");
Console.WriteLine(ee.Message.ToString());
Console.WriteLine(ee.InnerException.ToString());
Console.ReadKey();
}
return emailInformations;
}

以下是我需要使用队列中的每个项目异步调用的过程

static void AddAttachment(string  subject ,string docId, string user, string fileName)
{
var url = new StringBuilder();
url.Append(string.Format("https://webdemo-t.test.com:8443/Services/Service/MyService.svc/AddAttachment?User={0}&Engagement={1}&FileName={2}&DocumentTrasferID={3}", user, subject, fileName, docId));

Console.WriteLine(url.ToString());
WebRequest request = WebRequest.Create(url.ToString());
var credential = new NetworkCredential("user", "xxxx", "xxxx");
request.Credentials = credential;
WebResponse ws = request.GetResponse();
Encoding enc = System.Text.Encoding.GetEncoding(1252);
var responseStream = new StreamReader(ws.GetResponseStream());
string response = responseStream.ReadToEnd();
responseStream.Close();
Console.WriteLine(response);
}

最佳答案

如果您的 .NET 是 4.0 或 smth,您是否尝试过 AsParallel 扩展方法?

关于c# - 如何将 IEumerable 集合添加到队列并在 .NET 中异步处理每个项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13947224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com