Error calling async task in winforms load event c #

2

I have an application in C # that when displaying a certain window performs several operations in its Load () method, since several tasks take some time and I have tried to load animation but I could not, and tried to put the Load method () of the asynchronous form but it still does not work, my load method is this:

private async void FormMembresia_LoadAsync(object sender, EventArgs e)
    {
        pcloader.Visible = true;
        await DescNodoLoading();
        pcloader.Visible = false;
    }

From here I call the method DescNodoLoading () which is the one that executes the tasks (Consult an API, local files and finally graph), it is a method like this:

private Task<Boolean> DescNodoLoading()
    {
        return Task.Run(() =>
        {
            BeginInvoke(new Action(() =>
            {
             //CODE
            }
         }
       }

When opening the window it does not finish loading 100% and it is blocked until the DescNodoLoading () method task finishes, it does not even show the load gif because it hangs until the method ends, could someone tell me why? does not the function run in the background while the form is created and shows me the load gif which is what I think should happen?

    
asked by DVertel 24.09.2018 в 05:17
source

2 answers

0

You must change to this:

private Task<Boolean> DescNodoLoading()
{
    return Task.Run(() =>
    {
         //CODE
     }
   }

BeginInvoke what it does is execute code in the thread of the UI, with which it is not asynchronous

    
answered by 25.09.2018 / 15:54
source
-1

The problem is that this is the job of Await , if you want to run in the background you should use Async , I'll explain:

  • Await : The await operator is applied to a task in an asynchronous method to Suspend the execution of the method until the expected task is completed.
  • Async : Use the async switch to specify a method, a lambda expression or an anonymous method is asynchronous. If this modifier is used in a method or expression, it is referenced as an asynchronous method.

These are the main types of asynchronous programming of C #.

    
answered by 24.09.2018 в 07:03