本文整理汇总了C#中ConcurrentBag.AsParallel方法的典型用法代码示例。如果您正苦于以下问题:C# ConcurrentBag.AsParallel方法的具体用法?C# ConcurrentBag.AsParallel怎么用?C# ConcurrentBag.AsParallel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ConcurrentBag
的用法示例。
在下文中一共展示了ConcurrentBag.AsParallel方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ListContacts
public override async Task ListContacts(ContactQuery query)
{
ContactLite[] returnUsers = null;
Task.Run(async () =>
{
var bExceptionRaised = false;
try
{
var finallist = new ConcurrentBag<ContactLite>();
var agenda = await ContactManager.RequestStoreAsync();
var foundContactList = await agenda.FindContactsAsync();
if (query != null)
{
WindowsPhoneUtils.Log("Listing contacts by query: " + query);
FilterReturnedContacts(foundContactList, finallist, query);
}
else
{
WindowsPhoneUtils.Log("Listing ALL contacts...");
foundContactList.AsParallel()
.ForAll(contact => finallist.Add(new ContactLite
{
ID = contact.Id.Replace("{", "").Replace(".", "").Replace("}", ""),
DisplayName = contact.DisplayName,
Name = contact.FirstName,
Firstname = contact.MiddleName,
Lastname = contact.LastName,
Phones = GetContactPhonesArray(contact),
Emails = GetContactEmailArray(contact)
}));
}
returnUsers = finallist.ToArray();
}
catch (UnauthorizedAccessException ex)
{
WindowsPhoneUtils.Log("Not enough privileges to access Contacts");
WindowsPhoneUtils.InvokeCallback(AccessDeniedContactsCallback, WindowsPhoneUtils.CALLBACKID, null);
return;
}
catch (Exception ex)
{
//error
bExceptionRaised = true;
}
if (bExceptionRaised)
{
try
{
_faultyLetters = new ConcurrentBag<string>();
_startingLetters = new ConcurrentBag<string>();
_finalUsers.Clear();
await StartContactSeach(String.Empty, query);
returnUsers = _finalUsers.Values.ToArray();
_startingLetters.AsParallel().ForAll(startingLetter =>
{
if (!_faultyLetters.Any(
faultySilabe =>
faultySilabe.StartsWith(startingLetter,
StringComparison.CurrentCultureIgnoreCase)))
_faultyLetters.Add(startingLetter);
});
WindowsPhoneUtils.InvokeCallback(WpContactsFailedCallback, WindowsPhoneUtils.CALLBACKID, JsonConvert.SerializeObject(_faultyLetters.OrderBy(x => x).ToArray()));
}
catch (Exception ex)
{
//UNHANDLED ERROR
WindowsPhoneUtils.Log("Unhandled error recovering contacts:" + ex.Message);
}
}
WindowsPhoneUtils.InvokeCallback(ListContactsCallback, WindowsPhoneUtils.CALLBACKID, JsonConvert.SerializeObject(returnUsers));
});
}
示例2: SearchContactsWithLetter
private async Task SearchContactsWithLetter(string sLetters, ContactQuery query)
{
var bExceptionRaised = false;
try
{
var agenda = await ContactManager.RequestStoreAsync();
var contacts = await agenda.FindContactsAsync(sLetters);
var finallist = new ConcurrentBag<ContactLite>();
FilterReturnedContacts(contacts, finallist, query);
finallist.AsParallel().ForAll(contact => _finalUsers.TryAdd(contact.ID, contact));
}
catch (Exception ex)
{
//Security Error --> '@' character in websites collection URL
if (ex.HResult == -2146697202)
{
bExceptionRaised = true;
}
}
if (bExceptionRaised)
{
if (sLetters.Length < 2)
{
_startingLetters.Add((sLetters));
await StartContactSeach(sLetters, query);
}
else
_faultyLetters.Add(sLetters);
}
}
示例3: binaryFilter
private ConcurrentBag<cluster> binaryFilter(ConcurrentBag<cluster> listIn, char mode)
{
ConcurrentBag<cluster> returnList = new ConcurrentBag<cluster>();
int amount;
if (listIn.Count % 2 == 0) // even
amount = (int)listIn.Count / 2;
else //odd
amount = (int)(listIn.Count + 1) / 2;
switch (mode)
{
case 'C':
{ //cost efficiency
List<cluster> sortedList = listIn.AsParallel().OrderBy(o => o.getPerformance_to_Cost_ratio()).ToList<cluster>();
Parallel.ForEach(sortedList, c =>
{
if (returnList.Count < amount)
returnList.Add(c);
});
break;
}
case 'S':
{ // space efficiency
List<cluster> sortedList = listIn.AsParallel().OrderBy(o => o.getPerformance_to_Space_ratio()).ToList<cluster>();
Parallel.ForEach(sortedList, c =>
{
if (returnList.Count < amount)
returnList.Add(c);
});
break;
}
case 'P':
{ //power efficiency
List<cluster> sortedList = listIn.AsParallel().OrderBy(o => o.getPerformance_to_Power_Usage()).ToList<cluster>();
Parallel.ForEach(sortedList, c =>
{
if (returnList.Count < amount)
returnList.Add(c);
});
break;
}
case 'M':
{ //max power
List<cluster> sortedList = listIn.AsParallel().OrderBy(o => o.getTotal_Performance()).ToList<cluster>();
Parallel.ForEach(sortedList, c =>
{
if (returnList.Count < amount)
returnList.Add(c);
});
break;
}
case 'Q':
{ //max power
List<cluster> sortedList = listIn.AsParallel().OrderByDescending(o => o.getClusterCost()).ToList<cluster>();
Parallel.ForEach(sortedList, c =>
{
if (returnList.Count < amount)
returnList.Add(c);
});
break;
}
}
return returnList;
}