<blockquote>
  <p>Note: I have no particular knowledge of how <a href="/questions/tagged/monobrick" class="post-tag" title="show questions tagged &#39;monobrick&#39;" rel="tag">monobrick</a> work.</p>
</blockquote>

<p>You're never escaping the <code>while (true)</code> 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:</p>



<pre><code>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) 
        {

        }
    }         
} 
</code></pre>

<p>But depending on the internals of <a href="/questions/tagged/monobrick" class="post-tag" title="show questions tagged &#39;monobrick&#39;" rel="tag">monobrick</a> this might not work at all. </p>

CC BY-SA 3.0
2014-02-07T07:56:16.343Z
2014-02-07T07:56:16.343Z
Stephane Delcroix
<p>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:</p>



<pre><code>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 ();  
        }             
    } 
}
</code></pre>

<p>But it does work with the example from the <a href="http://monobrick.dk" rel="nofollow">Monobrick</a> homepage:</p>



<pre><code>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();  
    }  
  }  
}
</code></pre>

<programming><ev3><monobrick>
EV3 Event Delegation
Benjamin Minixhofer
# EV3 Event Delegation

(Asked by: username_0 on 2014-02-05)

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 += () => {   
            stopped.Set();  
        };  
        buts.UpPressed += () => {   
            LcdConsole.WriteLine("Sensor value: " + sensor.ReadAsString());  
        };  
        buts.DownPressed += () => {   
            LcdConsole.WriteLine("Raw sensor value: " + sensor.ReadRaw());  
        };  
        buts.EnterPressed += () => {   
            modeIdx = (modeIdx+1)%modes.Length;  
            sensor.Mode = modes[modeIdx];  
            LcdConsole.WriteLine("Sensor mode is: " + modes[modeIdx]);  
        };    
        stopped.WaitOne();  
    }  
  }  
}

**Tags:** programming, ev3, monobrick


---

## 1 answers

### Answer by username_1 on 2014-02-07. Score: 1

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 += () => {continue = false;};
        while (continue) 
        {

        }
    }         
} 

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


---

