0

I have an "Update" function in which the end isn't executed, then I use the function "Console.WriteLine" to know where the function is interrupted. Here is the Udate function:

async Task Update()
    {
        count++;
        try
        {
            List<string> cmds = new List<string>();
            if (Ev3Messaging.IsConnected())
            {
                Tuple<string, string> response = null;
                Console.WriteLine("Test1"); // Displayed
                try
                {
                    response = await Ev3Messaging.ReceiveText(true);
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Source + ": " + ex.Message); // Not displayed
                }
                Console.WriteLine("Test2"); // Not displayed
                while (response != null)
                {
                    Console.WriteLine("Test2: " + response.Item2);
                    string[] list = response.Item2.Split(';');
                    foreach (string cmd in list)
                        cmds.Add(cmd);
                    response = await Ev3Messaging.ReceiveText(false);
                }
            }
            Canvas canvas = _screenSV.Holder.LockCanvas();
            if (canvas != null)
            {
                foreach (string cmd in cmds)
                    ExecuteCommand(cmd, canvas);
                _screenSV.Holder.UnlockCanvasAndPost(canvas);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Source + ": " + ex.Message);
        }
    }

The Update function is called each 100ms by the following code:

var timer = new System.Threading.Timer((e) =>
        {
            RunOnUiThread(async () => await Update());
        }, null, 0, 100);

It displays "Test1", but it displays neither the exception (no problem in the "EV3Messaging.ReceiveText" function), nor "Test2" (function interrupted before reaching "ShowAlert("Test2");" )

How is it possible ?

If you want me to post the "Ev3Messaging.ReceiveText" function, please ask me. I didn't post it since it is big enough, and I just want to know how is it possible to get this problem...

Thanks in advance.

  • How are you calling this method? And what does `ShowAlert` do, how does it work? – poke Apr 11 '17 at 12:25
  • 1
    Just set a breakpoint in VisualStudio and go trough the code line by line to see what happens. – FINDarkside Apr 11 '17 at 12:26
  • Well `Ev3Messaging.ReceiveText` either never returns or otherwise stops execution. I assume if you comment out that line then "Test2" is displayed? – Equalsk Apr 11 '17 at 12:26
  • or in Ev3Messaging.ReceiveText is a try{...} catch(Ex e) {} which simply swallows the ecxeption and for some reason the system cannot keep running... – Kris Apr 11 '17 at 12:27
  • Have you debugged this? If not..why wasn't that your first step? – rory.ap Apr 11 '17 at 12:28
  • Or ShowAlert throws an exception itself, or maybe even the thread is paused pending the result of `ReceiveText`. – Patrick Hofman Apr 11 '17 at 12:28
  • 2
    Your first step should be to make your function no longer be `async void`. `async void` makes the function fire and forget, so you remove any chance for proper error handling. I also would suggest you to use a different debugging functionality than displaying an alert box (which has to be triggered on the correct thread, so the correct async context needs to be restored—another point of possible failure). Write to the debug output instead to check if the function ever returns after the await. – poke Apr 11 '17 at 12:31
  • I debugged this, and I changed the "ShowAlert" to "Console.WriteLine" But I still get only the "Test1" notification... During the debugging, it seems that it is interrupted when it reached "await _socket.InputStream.ReadAsync(result, 0, 128);" in the EV3Messaging function. But I don't understand why it wouldn't throw an exception if there is a problem here... – Antoine Martin Apr 11 '17 at 12:49
  • `async void` cannot bubble up exceptions since they are fire & forget. – poke Apr 12 '17 at 09:35
  • I changed "async void Update()" to "async Task Update()", and in the timer, I changed "RunOnUiThread(Update);" to "RunOnUiThread(async () => await Update());" but the problem persists... I will edit the main message. – Antoine Martin Apr 12 '17 at 12:03

0 Answers0