Maui app async/await debugging issue

Haviv Elbsz 2,071 Reputation points
2022-11-24T12:45:32.35+00:00

Hi all

Debugging a sync app is straightforward but debugging
async/await is not.

Can someone direct me how to do that

Thank you very much

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,628 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Haviv Elbsz 2,071 Reputation points
    2022-11-24T14:43:55.94+00:00
     private async void RunButton_Clicked(object sender, EventArgs e)  
     {  
    
       progressBar.Progress = 0.0;  
    
       if(!IsAppRunning)  
       {  
         IsAppRunning = true;  
         RunButton.Text = "Cancel";  
         RunButton.TextColor = Colors.White;  
         RunButton.Background = Colors.Red;  
    
         m_cancelTokenSource = new CancellationTokenSource();  
    
         try  
         {  
    

    if (!DEBUG)

           if (actionOperator == "mul")  
             if (CheckIsThisASpatialMul())  
               await alfaStarsSpatialMul(m_cancelTokenSource.Token);  
             else  
               await alfaStarsMul(m_cancelTokenSource.Token);  
    
           else if (actionOperator == "div0")  
             await alfaStarsSpatialDiv(m_cancelTokenSource.Token);  
           else if (actionOperator == "div1")  
             await alfaStarsDiv1(m_cancelTokenSource.Token);  
           else if (actionOperator == "div2")  
             await alfaStarsDiv2(m_cancelTokenSource.Token);  
           else if (actionOperator == "div3")  
             await alfaStarsDiv3(m_cancelTokenSource.Token);  
           else if (actionOperator == "div4")  
             await alfaStarsDiv4(m_cancelTokenSource.Token);  
    
           else if (actionOperator == "addsub")  
             await alfaStarsAdSb(m_cancelTokenSource.Token);  
    
           else if (actionOperator == "add")  
             await alfaStarsAdd(m_cancelTokenSource.Token);  
    
           else if (actionOperator == "sub")  
             await alfaStarsSub(m_cancelTokenSource.Token);  
    
           else if (actionOperator == "sqr")  
             await alfaStarsSqr(m_cancelTokenSource.Token);  
    
           else  
           {  
             //thisMsg = "Wrong Input : \r\n";  
           }   
    

    endif

    if (DEBUG)

           if (actionOperator == "mul")  
             if (CheckIsThisASpatialMul())  
               alfaStarsSpatialMul();  
             else  
               alfaStarsMul();  
    
           else if (actionOperator == "div0")  
             alfaStarsSpatialDiv();  
           else if (actionOperator == "div1")  
             alfaStarsDiv1();  
           else if (actionOperator == "div2")  
             alfaStarsDiv2();  
           else if (actionOperator == "div3")  
             alfaStarsDiv3();  
           else if (actionOperator == "div4")  
             alfaStarsDiv4();  
    
           else if (actionOperator == "addsub")  
             alfaStarsAdSb();  
    
           else if (actionOperator == "add")  
             alfaStarsAdd();  
    
           else if (actionOperator == "sub")  
             alfaStarsSub();  
    
           else if (actionOperator == "sqr")  
             alfaStarsSqr();  
    
           else  
           {  
             //thisMsg = "Wrong Input : \r\n";  
           }  
    

    endif

         }  
         catch (OperationCanceledException)  
         {  
           //  
         }  
         finally  
         {  
    
           RunButton.Text = "Start";  
           RunButton.TextColor = Colors.White;  
           RunButton.Background = Microsoft.Maui.Graphics.Color.FromArgb("512BD4");  
           IsAppRunning = false;  
           m_cancelTokenSource = null;  
           progressBar.Progress = 0.0;  
    
         }  
       }  
       else  
       {  
         m_cancelTokenSource.Cancel();  
       }  
    
       return;  
    
     }  
    

    //-----------------------------------------------------------------------------

    if (DEBUG)

     public void alfaStarsSpatialMul()  
     {  
    

    endif

    if (!DEBUG)

     public Task alfaStarsSpatialMul(CancellationToken ct)  
     {  
       return Task.Run(() =>  
       {  
    

    endif

         try  
         {  
           int cnt;  
           int starsQty = 0;  
           int i;  
           ulong loopsCnt = 0;  
    
    
           for(combs = 0; combs < combsCnt; combs++)  
           {  
    

    if (!DEBUG)

             progValue = (double)combs / (double)combsCnt;            
             MainThread.BeginInvokeOnMainThread(() => { progressBar.Progress = progValue; });  
    

    endif

             iPN = 1;  
             while (iPN < PN)  
             {  
    
    
                 iPY = 0;  
                 while(iPY < PY)//*** while(iPY < PY) Algorithm section ******************  
                 {  
    

    if (!DEBUG)

                   if (ct.IsCancellationRequested)  
                   {     
                     stopwatch.Stop();  
                     strOutputTBText += $"\r\nCanceled at Loop {loopsCnt}";  
                     strOutputTBText += "\r\n";  
                     strOutputTBText += "Time elapsed:\r\n" + stopwatch.Elapsed;  
                     strOutputTBText += "\r\n";  
    
                     MainThread.BeginInvokeOnMainThread(() => {OutputTB.Text += strOutputTBText;});  
    
                     if (fromToCKB.IsChecked == false) IsFromToSetToZero = false;  
                     if(alphaCKB.IsChecked == false) IsAlphaMode = false;  
    
                     throw new OperationCanceledException(ct);  
                   }  
    

    endif

                 }// while(iPY < PY)  
    
    
             }// while (iPN < PN)  
    
    
    
           }//for(combs  
    
    
           strOutputTBText += $"\r\n {loopsCnt} Loops";  
           strOutputTBText += "\r\n";  
    
           stopwatch.Stop();  
           strOutputTBText += "Time elapsed:\r\n" + stopwatch.Elapsed;  
           strOutputTBText += "\r\n";  
           MainThread.BeginInvokeOnMainThread(() => {OutputTB.Text += strOutputTBText;});  
           NoSolution();  
         }  
         catch (Exception ex)  
         {  
           if(ex.ToString().Contains("OperationCanceledException"))  
           {  
             //  
           }  
           else  
           {  
             strOutputTBText += "\r\n\r\n";  
             strOutputTBText += $"Exception = " + ex + "\r\n";  
             strOutputTBText += $"Invalid Puzzle";  
             strOutputTBText += "\r\n\r\n";  
             MainThread.BeginInvokeOnMainThread(() => {OutputTB.Text += strOutputTBText;});  
           }  
         }  
         finally  
         {  
          if (fromToCKB.IsChecked == false) IsFromToSetToZero = false;  
          if(alphaCKB.IsChecked == false) IsAlphaMode = false;  
           progressBar.Progress = 0.0;  
         }  
    

    if (!DEBUG)

       }, ct);  
    

    endif

     }//alfaStarsSpatialMul()
    
    0 comments No comments

  2. Haviv Elbsz 2,071 Reputation points
    2022-11-24T14:54:05.67+00:00

    Notes: This async/await pattern was taken from Arlen Feldman's website

    Handling long operations with cancel and progress in C# with async
    https://cowthulu.com/handling-long-operations-with-cancel-and-progress-in-c-with-async/

    I addopt it to fit my need as shown below

    The #if #endif directives is for skiping async debuging

     private async void RunButton_Clicked(object sender, EventArgs e)  
     {  
    
       progressBar.Progress = 0.0;  
    
       if(!IsAppRunning)  
       {  
         IsAppRunning = true;  
         RunButton.Text = "Cancel";  
         RunButton.TextColor = Colors.White;  
         RunButton.Background = Colors.Red;  
    
         m_cancelTokenSource = new CancellationTokenSource();  
    
         try  
         {  
    

    if (!DEBUG)

           if (actionOperator == "mul")  
             if (CheckIsThisASpatialMul())  
               await alfaStarsSpatialMul(m_cancelTokenSource.Token);  
             else  
               await alfaStarsMul(m_cancelTokenSource.Token);  
    
           else if (actionOperator == "div0")  
             await alfaStarsSpatialDiv(m_cancelTokenSource.Token);  
           else if (actionOperator == "div1")  
             await alfaStarsDiv1(m_cancelTokenSource.Token);  
           else if (actionOperator == "div2")  
             await alfaStarsDiv2(m_cancelTokenSource.Token);  
           else if (actionOperator == "div3")  
             await alfaStarsDiv3(m_cancelTokenSource.Token);  
           else if (actionOperator == "div4")  
             await alfaStarsDiv4(m_cancelTokenSource.Token);  
    
           else if (actionOperator == "addsub")  
             await alfaStarsAdSb(m_cancelTokenSource.Token);  
    
           else if (actionOperator == "add")  
             await alfaStarsAdd(m_cancelTokenSource.Token);  
    
           else if (actionOperator == "sub")  
             await alfaStarsSub(m_cancelTokenSource.Token);  
    
           else if (actionOperator == "sqr")  
             await alfaStarsSqr(m_cancelTokenSource.Token);  
    
           else  
           {  
             //thisMsg = "Wrong Input : \r\n";  
           }   
    

    endif

    if (DEBUG)

           if (actionOperator == "mul")  
             if (CheckIsThisASpatialMul())  
               alfaStarsSpatialMul();  
             else  
               alfaStarsMul();  
    
           else if (actionOperator == "div0")  
             alfaStarsSpatialDiv();  
           else if (actionOperator == "div1")  
             alfaStarsDiv1();  
           else if (actionOperator == "div2")  
             alfaStarsDiv2();  
           else if (actionOperator == "div3")  
             alfaStarsDiv3();  
           else if (actionOperator == "div4")  
             alfaStarsDiv4();  
    
           else if (actionOperator == "addsub")  
             alfaStarsAdSb();  
    
           else if (actionOperator == "add")  
             alfaStarsAdd();  
    
           else if (actionOperator == "sub")  
             alfaStarsSub();  
    
           else if (actionOperator == "sqr")  
             alfaStarsSqr();  
    
           else  
           {  
             //thisMsg = "Wrong Input : \r\n";  
           }  
    

    endif

         }  
         catch (OperationCanceledException)  
         {  
           //  
         }  
         finally  
         {  
    
           RunButton.Text = "Start";  
           RunButton.TextColor = Colors.White;  
           RunButton.Background = Microsoft.Maui.Graphics.Color.FromArgb("512BD4");  
           IsAppRunning = false;  
           m_cancelTokenSource = null;  
           progressBar.Progress = 0.0;  
    
         }  
       }  
       else  
       {  
         m_cancelTokenSource.Cancel();  
       }  
    
       return;  
    
     }  
    

    //-----------------------------------------------------------------------------

    if (DEBUG)

     public void alfaStarsSpatialMul()  
     {  
    

    endif

    if (!DEBUG)

     public Task alfaStarsSpatialMul(CancellationToken ct)  
     {  
       return Task.Run(() =>  
       {  
    

    endif

         try  
         {  
           int cnt;  
           int starsQty = 0;  
           int i;  
           ulong loopsCnt = 0;  
    
    
           for(combs = 0; combs < combsCnt; combs++)  
           {  
    

    if (!DEBUG)

             progValue = (double)combs / (double)combsCnt;            
             MainThread.BeginInvokeOnMainThread(() => { progressBar.Progress = progValue; });  
    

    endif

             iPN = 1;  
             while (iPN < PN)  
             {  
    
    
                 iPY = 0;  
                 while(iPY < PY)//*** while(iPY < PY) Algorithm section ******************  
                 {  
    

    if (!DEBUG)

                   if (ct.IsCancellationRequested)  
                   {     
                     stopwatch.Stop();  
                     strOutputTBText += $"\r\nCanceled at Loop {loopsCnt}";  
                     strOutputTBText += "\r\n";  
                     strOutputTBText += "Time elapsed:\r\n" + stopwatch.Elapsed;  
                     strOutputTBText += "\r\n";  
    
                     MainThread.BeginInvokeOnMainThread(() => {OutputTB.Text += strOutputTBText;});  
    
                     if (fromToCKB.IsChecked == false) IsFromToSetToZero = false;  
                     if(alphaCKB.IsChecked == false) IsAlphaMode = false;  
    
                     throw new OperationCanceledException(ct);  
                   }  
    

    endif

                 }// while(iPY < PY)  
    
    
             }// while (iPN < PN)  
    
    
    
           }//for(combs  
    
    
           strOutputTBText += $"\r\n {loopsCnt} Loops";  
           strOutputTBText += "\r\n";  
    
           stopwatch.Stop();  
           strOutputTBText += "Time elapsed:\r\n" + stopwatch.Elapsed;  
           strOutputTBText += "\r\n";  
           MainThread.BeginInvokeOnMainThread(() => {OutputTB.Text += strOutputTBText;});  
           NoSolution();  
         }  
         catch (Exception ex)  
         {  
           if(ex.ToString().Contains("OperationCanceledException"))  
           {  
             //  
           }  
           else  
           {  
             strOutputTBText += "\r\n\r\n";  
             strOutputTBText += $"Exception = " + ex + "\r\n";  
             strOutputTBText += $"Invalid Puzzle";  
             strOutputTBText += "\r\n\r\n";  
             MainThread.BeginInvokeOnMainThread(() => {OutputTB.Text += strOutputTBText;});  
           }  
         }  
         finally  
         {  
          if (fromToCKB.IsChecked == false) IsFromToSetToZero = false;  
          if(alphaCKB.IsChecked == false) IsAlphaMode = false;  
           progressBar.Progress = 0.0;  
         }  
    

    if (!DEBUG)

       }, ct);  
    

    endif

     }//alfaStarsSpatialMul()