EV3 Event Delegation My problem is that the robot, which should just remain in the program until the Escape Button is pressed, just stays in the while-loop. The current code looks like this:



using System;  
using MonoBrickFirmware;  
using MonoBrickFirmware.Movement;  
using MonoBrickFirmware.Display;  
using MonoBrickFirmware.Sensors;  
using MonoBrickFirmware.UserInput;  
using System.Threading; 

namespace ColorSensorExample  
{  
    class R_Main  
    {  
        public static void Main (string[] args)
        {  
            R_Main main = new R_Main ();
        }

        public R_Main()
        {

            var sensor = new EV3ColorSensor (SensorPort.In2);  
            ButtonEvents buts = new ButtonEvents ();  
            buts.EscapePressed += new Action(onEscape);
            while (true) 
            {

            }
        }

        private void onEscape()
        {
            EventWaitHandle stopped = new ManualResetEvent (false);
            stopped.Set ();  
        }             
    } 
}


But it does work with the example from the Monobrick homepage:



using System;  
using MonoBrickFirmware;  
using MonoBrickFirmware.Movement;  
using MonoBrickFirmware.Display;  
using MonoBrickFirmware.Sensors;  
using MonoBrickFirmware.UserInput;  
using System.Threading;  
namespace ColorSensorExample  
{  
class MainClass  
{  
  public static void Main (string[] args)  
  {  
        EventWaitHandle stopped = new ManualResetEvent(false);  
        ColorMode[] modes = {ColorMode.Color, ColorMode.Reflection,   
                             ColorMode.Ambient, ColorMode.Blue};  
        int modeIdx = 0;  
        var sensor = new ColorSensor(SensorPort.In1);  
        ButtonEvents buts = new ButtonEvents ();  

        buts.EscapePressed += () =&gt; {   
            stopped.Set();  
        };  
        buts.UpPressed += () =&gt; {   
            LcdConsole.WriteLine("Sensor value: " + sensor.ReadAsString());  
        };  
        buts.DownPressed += () =&gt; {   
            LcdConsole.WriteLine("Raw sensor value: " + sensor.ReadRaw());  
        };  
        buts.EnterPressed += () =&gt; {   
            modeIdx = (modeIdx+1)%modes.Length;  
            sensor.Mode = modes[modeIdx];  
            LcdConsole.WriteLine("Sensor mode is: " + modes[modeIdx]);  
        };    
        stopped.WaitOne();  
    }  
  }  
}
Note: I have no particular knowledge of how monobrick work.


You're never escaping the while (true) loop. The example you're pointing too use an event-based model, and it looks like you prefer a loop. This is what you want:



class R_Main  
{  
    public static void Main (string[] args)
    {  
        R_Main main = new R_Main ();
    }

    public R_Main()
    {

        var sensor = new EV3ColorSensor (SensorPort.In2);  
        bool continue = true;
        ButtonEvents buts = new ButtonEvents ();  
        buts.EscapePressed += () =&gt; {continue = false;};
        while (continue) 
        {

        }
    }         
} 


But depending on the internals of monobrick this might not work at all.