Reply To: Ending the Program by pressing the Escape-Button

HomeForumsMonoBrick EV3 FirmwareEnding the Program by pressing the Escape-ButtonReply To: Ending the Program by pressing the Escape-Button

#3942
Author Image
Jacek S
Participant

Hi,
Your program never ends because you must start main loop in separate thread and place wait event at the end of main thread.


using System.Threading;
using System.Threading.Tasks;
using MonoBrickFirmware.UserInput;

namespace EV3Program
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            ManualResetEvent terminateProgram = new ManualResetEvent(false);
            ButtonEvents buts = new ButtonEvents();

            buts.EscapePressed += () =>
            {
                terminateProgram.Set();
            };

            var myLogic = new MyLogic();

            Task.Factory.StartNew(() => myLogic.Run());

            terminateProgram.WaitOne();
        }
    }
}

Jacek

Posted in

Make a donation