本文介绍了如何在不等待先前执行的情况下并行运行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞!我创建了一个Azure C#作业,以便为多名乘客提供飞行服务。I have created a Azure C# job to load funds for multiple passengers flight wise.在一分钟的那份工作中,有多个航班可以加载乘客。我已经编写了一个代码来处理它,但是在执行时,它正在同步运行,我想执行要处理的功能,而不是等待前一个完成。In that job in a minute there are multiple flights with passengers are available to load. I have written a code to process it, but while execution, it is running synchronously, I want to execute the functionality to process without waiting for previous one to finish.让我按代码解释,公共类PassengerFlightDetails { &NBSP; &NBSP; &NBSP; int FlightId 列表<乘客> lstPassengers {get; set;}; int AmountToLoad {get; set;} } 公共类ProcessCards { public void LoadFunds(int flightid,List< Passengers> passenger,int AmountToLoad) { foreach(旅客中的var项目) { int passengerid = passengers.passengerid; LoadFundToPassenger(flightid,passengerid,AmountToLoad) } } public bool LoadFundToPassenger(int flightid,int passengerid,int AmountToLoad) { //加载功能 } } 公共类MainProgram { public bool FunctionToProcessFlightPassgengers() { 列表< PassengerFlightDetails> lstPassengerFlight = new List< PassengerFlightDetails>(); //考虑lstPassengerFlight包含10个不同的记录, //例如lstPassengerFlight [0] lstPassengerFlight.FlightId = 1; lstPassengerFlight.lstPassengers = 150; lstPassengerFlight.AmountToLoad = 10; //例如lstPassengerFlight [1] lstPassengerFlight.FlightId = 2; lstPassengerFlight.lstPassengers = 120; lstPassengerFlight.AmountToLoad = 15; //例如lstPassengerFlight [2] lstPassengerFlight.FlightId = 3; lstPassengerFlight.lstPassengers = 200; lstPassengerFlight.AmountToLoad = 20; //类似于上面的记录列表 - lstPassengerFlight根据FlightId包含不同的记录。 // LoadFunds - 以乘客方式加载资金的活动 foreach(lstPassengerFlight中的var passflight) { ProcessCards objProcessCards = new ProcessCards(); objProcessCards.LoadFunds(passflight.FlightId,passflight.lstPassengers,passflight.AmountToLoad ); $ } //在for循环中,我想并行处理LoadFunds活动。 //即。在循环中首先它将加载航班ID 1的资金,它将等待完成,然后它将转到航班ID 2和 &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; //所以&NBSP; on。b //现在我不想等到1号航班结束,2号航班完成,3号航班完成。 //我想并行启动LoadFunds资金流程,意味着在循环中它将调用loadfunds功能,它不会等到完成后,它将立即处理下一次航班。 } &NBSP; }public class PassengerFlightDetails{ int FlightIdList<Passengers> lstPassengers {get;set;} ;int AmountToLoad {get;set;}}public class ProcessCards{public void LoadFunds(int flightid, List<Passengers> passengers,int AmountToLoad){foreach(var item in passengers){int passengerid = passengers.passengerid;LoadFundToPassenger (flightid,passengerid,AmountToLoad)}}public bool LoadFundToPassenger(int flightid, int passengerid,int AmountToLoad){// Load functionality}}public class MainProgram{public bool FunctionToProcessFlightPassgengers(){List<PassengerFlightDetails> lstPassengerFlight = new List<PassengerFlightDetails>();// Consider lstPassengerFlight contains 10 different records, // for e.g. lstPassengerFlight[0]lstPassengerFlight.FlightId = 1;lstPassengerFlight.lstPassengers = 150;lstPassengerFlight.AmountToLoad = 10;// for e.g. lstPassengerFlight[1]lstPassengerFlight.FlightId = 2;lstPassengerFlight.lstPassengers = 120;lstPassengerFlight.AmountToLoad = 15;// for e.g. lstPassengerFlight[2]lstPassengerFlight.FlightId = 3;lstPassengerFlight.lstPassengers = 200;lstPassengerFlight.AmountToLoad = 20;// similar to above records list - lstPassengerFlight contains different records as per FlightId wise// LoadFunds - Activity to load funds passenger wiseforeach(var passflight in lstPassengerFlight){ProcessCards objProcessCards = new ProcessCards();objProcessCards.LoadFunds(passflight.FlightId,passflight.lstPassengers,passflight.AmountToLoad);}//In the for loop, I want to process LoadFunds activity in parallel.// i.e . in the loop first it will load the funds for flight id 1, it will wait to complete, then it will go for flight id 2 and //so on.// Now I don't want to wait till flight 1 finish, flight 2, flight 3 to finish.// I want to start LoadFunds funds process in parallel, means in the loop it will call loadfunds functionality and it will not wait till complete, immediately it will process for next flight.} }请建议我执行"LoadFunds"的最佳方法。功能并行i,e执行它而不等待第一个完成。 Kindly suggest me the best approach to execute "LoadFunds" functionality in parallel i,e execute it without waiting for first one to finish. 推荐答案 考虑 Parallel.ForEach : https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel-foreach-loop 。ConsiderParallel.ForEach:https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-simple-parallel-foreach-loop. 这篇关于如何在不等待先前执行的情况下并行运行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-30 02:36