Click here to Skip to main content
14,366,562 members
Rate this:
Please Sign up or sign in to vote.
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100