Click here to Skip to main content
15,850,751 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
as follow ,what's the best methods for use async/await for Multiple Task :

semicode:

public class Process
{
   public Process()
   {
       this.ProcessItem();
   }
}

  private async void ProcessItem()
   {
       while (true)
       {
           await this.FetchDeviceData(); //Read From Devices
           await Task.Delay(2000);   //300000
           await this.Process(); //Insert Into DataBase
       }
   }

   private async Task FetchDeviceData()
   {
       await Task.Run(() =>
       {
        //Read From all Devices;
       }
   }

  private async Task Process()
  {
     if (!(await RDataBase.ProcessItem(Mem,Date)))
         //Update Record;
  }

        //---------------------- RDataBase Class

  public static async Task<bool> ProcessItem(int Memb, int Date)
     {
       return await Task.Run(() =>
        {
            try
            {
            //Array List = Read Device Info From DataBase
            return True;
            }
            catch
            {
          return false;
            }
        });
     }


What I have tried:

I Call the Instance Of Process Class in Global.ascx in the Application_Start() method.
Posted
Updated 16-Jan-18 20:54pm
v3
Comments
David_Wimbley 16-Jan-18 16:21pm    
Are these tasks dependent upon each other or cant hey run in parallel?
bernova 17-Jan-18 0:16am    
they Should be run simultaneously

1 solution

Here is one way you could achieve what I think you are wanting to do.

 var tasks = new List<Task>();

 tasks.Add(Task.Factory.StartNew(() =>
{
   // add long running task here - 1
}));
				
 tasks.Add(Task.Factory.StartNew(() =>
{
   // add long running task here - 2
}));

Task.WaitAll(tasks.ToArray());


You could replace Task.Factor.StartNew with async method calls.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900