locked
Parallel Programming using await Task.WhenAll ,task not working in parallel RRS feed

  • Question

  • User-708726653 posted

    Hi All,

    I am currently facing an issue with parallel programming using   await Task.WhenAll . 

    i have code as below

    Task<DataSet[]> dsFinalDataset = Task.Run(async () => await myfunction();

    ==============

    public static async Task<DataSet[]>myfunction()

    {

    Task<DataSet>[] AllTask = null;

    Task<DataSet> ts1= null;

    Task<DataSet> ts2= null;

    for (int s = 0; s <2; s++)
    {

    ts1=firstFunction();

    Array.Resize(ref AllTask, (AllTask != null ? AllTask.Length + 1 : 1));
    AllTask[AllTask.Length - 1] = ts1;

    }

    for (int s = 0; s <2; s++)
    {

    ts2=secondFunction();

    Array.Resize(ref AllTask, (AllTask != null ? AllTask.Length + 1 : 1));
    AllTask[AllTask.Length - 1] = ts2;

    }

    return await Task.WhenAll<DataSet>(AllTask);

    public static async Task<DataSet> firstfunction()

    {

    dataset ds=new dataset();

    await Task.Run(() =>
    {
    ds= // get data from http call
    });

    return ds;

    }

    public static async Task<DataSet> secondfunction()

    {

    dataset ds=new dataset();

    await Task.Run(() =>
    {
    ds= // get data from http call
    });

    return ds;

    }

    ======================================================================

    this code takes time to execute.

    suppose firstfunction() hit to times and takes 2 sec each (2+2)

    secondfunction() hit to times and takes 2 sec each(2+2)

    then whole function takes  8 sec to execute 

    please assist me to reduce the time to execute main function

    Saturday, June 23, 2018 7:18 AM

All replies

  • User36583972 posted


    Hi animesh619,

    this code takes time to execute.

    suppose firstfunction() hit to times and takes 2 sec each (2+2)

    secondfunction() hit to times and takes 2 sec each(2+2)

    then whole function takes  8 sec to execute 

    please assist me to reduce the time to execute main function

    Please try the following code structure and modifies your code. For each task, we can use Task.Run method to open new resources. Parallel execution. The maximum waiting time is the longest task time.

    class Program
        {
            static  System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            static void Main(string[] args)
            {
    
                sw.Start();
                Task t = AsynchronousProcessing();
                t.Wait();   
                sw.Stop();
                TimeSpan ts2 = sw.Elapsed;
                Console.WriteLine("example2 time {0}", ts2.TotalMilliseconds);
                Console.Read();
            }
    
    
            async static Task AsynchronousProcessing()
            {
                Task<string> t1 = GetInfoAsync("Task 1", 3);
                Task<string> t2 = GetInfoAsync("Task 2", 5);
                string[] results = await Task.WhenAll(t1, t2);
                foreach (string result in results)
                {
                    Console.WriteLine(result);
                }
            }
    
            async static Task<string> GetInfoAsync(string name, int seconds)
            {
                await Task.Run(() => Thread.Sleep(TimeSpan.FromSeconds(seconds)));  
                return string.Format("Task {0} is running on a thread id {1}. Is thread pool thread: {2}",
                name, Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.IsThreadPoolThread);
            }
    
        }


    Best Regards,

    Yong Lu

    Monday, June 25, 2018 6:34 AM