.netcore 使用多线程Task消费队列进行消费

2021-04-11 14:27

阅读:303

标签:bat   tuple   数据库查询   ali   syn   队列   span   str   async   

  WebApiResult为封装公用方法,不是微软自带,此段代码指是做画龙点睛作用,添加队列、异步消费等

  public async Task AuthentDeal()
        {
            //从数据库查询要处理的队列
            var batchList = _MySqlMasterDb.tab1.Where(p => p.is_certifi == -1).ToList();
            if (batchList.Count 0)
            {
                return;
            }

            Dictionary resultList = new Dictionary();
            //将查询对象放入队列
            ConcurrentQueue searchQueue = new ConcurrentQueue();
            foreach (var item in batchList)
            {
                searchQueue.Enqueue(item);
            }
            //设置线程数量
            int threadCount = searchQueue.Count  searchQueue.Count : Environment.ProcessorCount;
            Task[] tasks = new Task[threadCount];
            for (int i = 0; i )
            {
                tasks[i] = Task.Run(() =>
                {
                    WebApiResult taskResult = new WebApiResult();
                    while (searchQueue.TryDequeue(out partners t))
                    {
                        taskResult = VerifyPartners(t).Result;//验证方法
                        if (taskResult != null)
                        {
                            resultList.Add(t.id, taskResult);//验证结果存入字典中
                        }
                    }
                    return taskResult;
                });
            }
            Task.WaitAll(tasks);

            foreach (var item in batchList)
            {
                if (resultList.ContainsKey(item.id))
                {
                    var verifyResult = resultList[item.id];

                    var authResult = JsonConvert.DeserializeObjectbool, List, TencentBankQueryResult, string, bool>>(JsonConvert.SerializeObject(verifyResult.Data));
                //验证完成后业务处理
               //
              //
    
                }
            }
          
        }                   

 

.netcore 使用多线程Task消费队列进行消费

标签:bat   tuple   数据库查询   ali   syn   队列   span   str   async   

原文地址:https://www.cnblogs.com/personblog/p/13359017.html


评论


亲,登录后才可以留言!