当前位置: 首页>>代码示例>>C#>>正文


C# ConcurrentBag.AsParallel方法代码示例

本文整理汇总了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));
            });
        }
开发者ID:Appverse,项目名称:appverse-mobile,代码行数:78,代码来源:WindowsPhonePim.cs

示例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);
     }
 }
开发者ID:Appverse,项目名称:appverse-mobile,代码行数:30,代码来源:WindowsPhonePim.cs

示例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;
        }
开发者ID:prossouw79,项目名称:Cluster-Generator-VS,代码行数:74,代码来源:frm_CDT.cs


注:本文中的ConcurrentBag.AsParallel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。