none
c# and async methods RRS feed

  • Question

  • I am calling an async methods, which in side of it calling an external dll (bout from third party)

    this method which in the dll is  called using await (be definition).

    is there a way to check how many async methods were called and not finished yet?

    this is my code :

    SendData(list);
    
    private async void SendData(List<SomeObject> objlist){
    foreach(..... objlist){
    ….
    Submit[] results = await _c.Submit(somelist.ToArray());
    }     
    }

    Tuesday, August 13, 2019 11:51 AM

All replies

  • Hi,

    I'm not sure if I understand your question, but let me try to explain what I think it is you are looking for.

    If you call await on an asynchronous method, you are waiting until the returned Task is completed. Your SendData method does not return a Task (which you should do if possible). If you change the return type of your SendData method to Task, you can also await when calling the SendData method, OR, you can save the Task in a local variable, or even add it to a List<Task> in order to verify how many calls have been made:

    List<Task> runningTasks = new List<Task>();
    
    for( int i=0 ; i<10 ; i++ )
    {
        runningTasks.Add( SendData(list) );
    }
    
    
    
    
    private async Task SendData(List<SomeObject> objlist)
    {
        foreach(..... objlist)
        {
            ….
            Submit[] results = await _c.Submit(somelist.ToArray());
        }     
    }

    Kind regards,

    Johnny Hooyberghs

    Tuesday, August 13, 2019 12:03 PM
  • Hi,

    To add to your question:

    You can verify the status of all tasks or you can wait for all of them to finish:

    List<Task> runningTasks = new List<Task>();
    
    for( int i=0 ; i<10 ; i++ )
    {
        runningTasks.Add( SendData(list) );
    }
    
    
    await Task.WhenAll( runningTasks );
    
    // OR
    
    foreach( var runningTask in runningTasks )
    {
        Console.WriteLine( runningTask.Status );
    }

    Kind regards,

    Johnny Hooyberghs

    Tuesday, August 13, 2019 12:17 PM
  • Hello,

    See the following post for various method to work with status. See also, A tour of Task part 3 status.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, August 13, 2019 1:12 PM
    Moderator
  • Hi want 2 Learn, 

    Thank you for posting here.

    As Hooyberghs Johnny suggested, your method needs to return a Task so you can check for status.

    I use Task.IsCompleted property to make a test. Here’s my code.

    Main function:

                List<Task> lt = new List<Task>();
                int a = 0;
                while (a<20)
                {
                    lt.Add(SendData(list));             
                    a++;
                }
                Console.WriteLine(check(lt));

    Other Functions:

            private static int check(List<Task> listTask)
            {
                int b = 0;
                foreach (Task t in listTask)
                {
                    if (t.IsCompleted.Equals(false))
                    {
                        b++;
                    }
                }
                return b;
            }
    
            private static async Task SendData(List<SomeObject> objlist)
            {
                //...
            }

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    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.

    Wednesday, August 14, 2019 9:58 AM
  • updating my first code, it runs inside a thread, which every 1 seconds calls the db,

    and if there is a data then the SendData(list) is called.

    how can I monitor in this case all running async and there status, and if one of them finished to remover it from the list?

    Wednesday, August 14, 2019 11:59 AM
  • Hi want 2 Learn, 

    Thanks for your feedback.

    >>it runs inside a thread, which every 1 seconds calls the db

    Could you provide some related code here? It will help us to analyze your problem and make a test.

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    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, August 16, 2019 8:26 AM
  • Hi, here is the code  :

    Thread _thread;
     _thread = new Thread(DoWork);
     _thread.Start();
    
    private async void DoWork()
            {
    while(true){
                List<GetDataForSendingWithExtResult> list= SenderDataLayer.GetDataForSending();
    if (list.Count>0
    SendData(list);
    }}
    
    private async void SendData(List<SomeObject> objlist){
    foreach(..... objlist){
    ….
    Submit[] results = await _c.Submit(somelist.ToArray());
    }     
    }

    Monday, August 19, 2019 8:24 AM
  • Hi,

    Just see my code example from before.

    Do not await on the Submit method, but store the resulting Task<Submit[]> in a List.

    Now you can foreach the List and check the Tasks statusses and the Result on each Task.

    Kind regards,

    Johnny Hooyberghs


    Monday, August 19, 2019 9:44 AM