Uses are waiting with Task.Run, but is the UI still hanging for a few seconds?

enter image description here I am using SAP.NET Connector 3.0 and trying to log in using a separate thread so that I can display the user interface for my kind of login animation.

I use Async and Await to start the login, but the user interface freezes for about 10 seconds during the login.

Here is the code, this is rude because I am developing a program quickly.

async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    Busy.Visibility = System.Windows.Visibility.Visible; // Shows progress animation

    if (await SAPLogin()) // Waits for login to finish, will always be true at the moment
    {
        await GetData(); // does things with sap

        Busy.Visibility = System.Windows.Visibility.Collapsed; // Hides progress animation
    }
 }


private Task<bool> SAPLogin()
{
    bool LoggedIn = true;

    return Task.Run(() =>
        {
           Backend = new BackendConfig();
           RfcDestinationManager.RegisterDestinationConfiguration(Backend);
           SapRfcDestination = RfcDestinationManager.GetDestination(MyServer);  // MyServer is just a string containing sever name

           SapRap = SapRfcDestination.Repository;

           BapiMD04 = SapRap.CreateFunction("MD_STOCK_REQUIREMENTS_LIST_API");

           BapiMD04.SetValue("WERKS", "140");

                return LoggedIn;
         });
}      

Can I just imagine that something in the Task is using the user interface?

1: , , GetData(). GetData() SAP ( ). , , , " " ​​ " ". , , , " ". . , 5 .

2: ,

SapRfcDestination = RfcDestinationManager.GetDestination(MyServer);

3: ​​ , , .

+3
1

, GetData Task.Run SAPLogin Dispatcher.Invoke, Dispatcher.BeginInvoke Dispatcher.InvokeAsync. ​​.

, . , Task.Factory.StartNew TaskCreationOptions.LongRunning Task.Run GetData ( async, .Unwrap() ). , , , , .

async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    Busy.Visibility = System.Windows.Visibility.Visible; // Shows progress animation

    if (await SAPLogin()) // Waits for login to finish, will always be true at the moment
    {
        //await GetData(); // does things with sap
        await Task.Factory.StartNew(() => GetData(),
            CancellationToken.None,
            TaskCreationOptions.LongRunning,
            TaskScheduler.Default).Unwrap();

        Busy.Visibility = System.Windows.Visibility.Collapsed; // Hides progress animation
    }
}

private Task<bool> SAPLogin()
{
    bool LoggedIn = true;

    return Task.Factory.StartNew(() =>
    {
        Backend = new BackendConfig();
        RfcDestinationManager.RegisterDestinationConfiguration(Backend);
        SapRfcDestination = RfcDestinationManager.GetDestination(MyServer);  // MyServer is just a string containing sever name

        SapRap = SapRfcDestination.Repository;

        BapiMD04 = SapRap.CreateFunction("MD_STOCK_REQUIREMENTS_LIST_API");

        BapiMD04.SetValue("WERKS", "140");

        return LoggedIn;
    }, 
    CancellationToken.None,
    TaskCreationOptions.LongRunning,
    TaskScheduler.Default);
}
+2

All Articles