locked
foreach problem in async method RRS feed

  • Question

  • User1231912896 posted

    Hi guys,
    in my blazor component (.razor) I have implemented this method to update database. It read a .txt file, and in a foreach loop it call my post web API to save data in the database.

        private async Task updateDatabaseAsync()
        {
            try
            {
                [ ... code ... ]
                
                // Update Database (post API)
                //
                int intTotal = 11;
                List<string> lstMyList = File.ReadLines(strPathNameText).Reverse().Take(intTotal).Reverse().ToList();
                //
                string sep = "\t";
                foreach (string txtMyDataInList in lstMyList)
                {
                    MyData newData = new MyData();
                    //
                    newData.TypeData01 = txtMyDataInList.Split(separator: sep.ToCharArray())[0];
                    newData.TypeData01 = txtMyDataInList.Split(separator: sep.ToCharArray())[1];
                    newData.TypeData01 = txtMyDataInList.Split(separator: sep.ToCharArray())[2];
                    //
                    await MyService.PostUpdateDatabase(newData);
                }
            }
            catch (Exception ex)
            {
                var errorMessage = ex.Message;
                throw;
            }
        }
    

    The method works fine.

    The problem is this: I have moved this method in a generic class (utility class) in order to use it in different parts of my code.

    Well, insie my class the method (the SAME method) doesn't works! In the line were the post web api is colled I receive an error: "object reference not set to an instance of an object".

    I have changed the method in different way, using "Task.WhenAll(tasks);" instead of the foreach loop, but nothing I receive the same error.

    foreach (var txtEstrazione in lstUltimeEstrazioni)
    {
        tasks.Add(Task.Run(async () =>
        {
                    MyData newData = new MyData();
                    //
                    newData.TypeData01 = txtMyDataInList.Split(separator: sep.ToCharArray())[0];
                    newData.TypeData01 = txtMyDataInList.Split(separator: sep.ToCharArray())[1];
                    newData.TypeData01 = txtMyDataInList.Split(separator: sep.ToCharArray())[2];
                    //
                    await MyService.PostUpdateDatabase(newData);
        }));
    }
    Task.WhenAll(tasks);
    tasks = null;

    I have also tried Parallel, but nothing again. Any idea to solve this problem? Thanks.

    Igor.

    Thursday, February 4, 2021 8:25 PM

Answers

  • User1231912896 posted

    Hello to everybody,

    I found the solution, the problem were in the MyService. I have injected the service in my class in wrong way. 

    I need to study more of Blazor, I think.

    Best regards to all.

    Igor.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 6, 2021 2:14 PM

All replies

  • User-474980206 posted

    your code makes no sense. why create a new task (thread) to run an async task? where do you await the Task.WhenAll()?  

    Friday, February 5, 2021 6:03 PM
  • User1231912896 posted

    Hi Bruce,

    sorry but I don’t understand your doubts, I’m new in .net core and don’t know sync and async as well to understand.

    I think you are speaking about the second version of my code, isn’t it? I was inspired by some tutorial or post viewed somewhere, and I have made lots of tests and changes.

    Do you have some suggestions to me? 

    Friday, February 5, 2021 9:28 PM
  • User1231912896 posted

    Hello to everybody,

    I found the solution, the problem were in the MyService. I have injected the service in my class in wrong way. 

    I need to study more of Blazor, I think.

    Best regards to all.

    Igor.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, February 6, 2021 2:14 PM