locked
How to run an action in parallel without waiting for previous execution RRS feed

  • Question

  • Hi,

    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.

    Let me explain by code,

           

    public class PassengerFlightDetails
    {
            int FlightId
    List<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 wise

    foreach(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.

    }      }

    Kindly suggest me the best approach to execute "LoadFunds" functionality in parallel i,e execute it without waiting for first one to finish. 

    Wednesday, December 19, 2018 8:55 AM

All replies

  • Wednesday, December 19, 2018 11:07 AM
  • Hi st0rch,

    Thank you for posting here.

    For your question, you want to run an action in parallel without waiting for previous execution.

    1.The lstPassengerFlight list need to be placed outside of the method body.

    2.I create the pa list to load LoadFunds method.

    public void loop()
            {
                MainProgram main = new MainProgram();
                List<Passengers> pa = new List<Passengers>();
                Parallel.ForEach(main.lstPassengerFlight, item =>
                 {
                     LoadFunds(item.FlightId, pa, item.AmountToLoad);
                 }
                );
            }
    

    Best regards,

    Jack J Jun.



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 21, 2018 8:35 AM