Wait for async Task for first method in xamarin froms

Prashant Sharma 66 Reputation points
2021-04-06T17:51:43.44+00:00

I am using async and await in my project and my first method is running in second method for ex- when data will save then print command will run print is depend on save and print command must run after save , but in my code sometime print command do not wait for save command execute ,that why I am getting deadlock error and Violation of PRIMARY KEY constraint in database. I am sharing my code. Thank u for help

    public async void ExecutePrintCommand()
       {
           UserDialogs.Instance.ShowLoading("Saving", MaskType.Clear);
           await Task.Delay(1000);

          try
           {

            var SaveStatus = await Save();


            if SaveStatus == 1)
            {

                UserDialogs.Instance.ShowLoading("Printing");
                await Task.Delay(1000);

                await Print();


                await App.Current.MainPage.Navigation.PushAsync(new RootPage());

            }
            else
            {
                Device.BeginInvokeOnMainThread(() =>
                {
                    navigation.PopAsync();

                });
            }


        }
Developer technologies | .NET | Xamarin
Developer technologies | C#
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.