locked
Parallel.Tasks slow. What am I doing wrong RRS feed

  • Question

  • User1857954827 posted

    hi team,

    kindly find my below sample code for then same.

    public static void HotelSearchAsynchronousNewV2(SearchParameter SearchPara) //first call for hotelsearch
    {

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

    }

    public static async Task<DataSet[]> HotelSearchAsyncNewV2()
    {

    Task<DataSet>[] AllTask = null;

    #region "DOTW"
    Task<DataSet> tsDOTW = null;
    #endregion

    for (int a = 0; a < splittedtables.Length; a++)
    {

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

    }

    public static async Task<DataSet> GetDOTWDataset()
    {
    DataSet DOTWDataSet = new DataSet();
    try
    {

    Hotel objHotel = new Hotel();
    await Task.Run(() =>
    {
    DOTWDataSet = objHotel.XMLDOTWPriceOnly();
    });

    return DOTWDataSet;
    }
    catch (Exception ex)
    {
    ExceptionManager.LogException(ex);
    return null;
    }
    finally
    {
    if (DOTWDataSet != null)
    DOTWDataSet.Dispose();
    }
    }

    public DataSet XMLDOTWPriceOnly()
    {

    // all logic goes here   

    }

    i would also like to know that dose Parallel.Tasks is also depend on CPU core or not.

    Monday, June 25, 2018 1:11 PM

Answers

  • User1857954827 posted

    Hi I have find out one of the good solution, which me be help some one just find bellow link for the same

    https://markheath.net/post/constraining-concurrent-threads-csharp

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 28, 2018 11:09 AM

All replies

  • User475983607 posted

    It seems like you wrapped synchronous code in a Parallel task.  That's not going to work.   Basically, you're not following recommended and openly published async programming patterns.   Plus the code has syntax errors and does not compile.

    Start by reading the docs to get a understanding of the syntax and patterns.  

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

    i would also like to know that dose Parallel.Tasks is also depend on CPU core or not.

    It depends on your code.  If the parallel task is CPU dependant then yes, otherwise no.

    https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/task-based-asynchronous-programming

    Monday, June 25, 2018 1:34 PM
  • User1857954827 posted

    Hi mgebhard ,

    Thanks for your replay can you please let me know at which part of my code is synchronous.  May be my code is not working as it's just a same code. from my entire code 

    Tuesday, June 26, 2018 4:31 AM
  • User475983607 posted

    chirag.jdk

    Hi mgebhard ,

    Thanks for your replay can you please let me know at which part of my code is synchronous.  May be my code is not working as it's just a same code. from my entire code 

    The main entry point is synchronous.  The following bit of code within the main entry point creates blocking threads.

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

    From this point on the code gets worse and worse.  The HotelSearchAsyncNewV2 method does not compile.  There are globally scoped variables, methods calls that are not async, and another Task.Run within GetDOTWDataset().  Again, more blocking threads.

    The code has the earmarks of synchronous code converted to asynchronous by adding async to method declarations then adding Task.Run to remove the compiler warnings.  This design strategy simply will not work.  The methods must be asych from top to bottom and without Task.Run.  Anyway, take the time to learn async/await patterns.  See the links in my first post.

    Tuesday, June 26, 2018 10:52 AM
  • User1857954827 posted

    Hi I have find out one of the good solution, which me be help some one just find bellow link for the same

    https://markheath.net/post/constraining-concurrent-threads-csharp

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 28, 2018 11:09 AM