I am using C# in the .Net Framework 8.0. I want to escape while loop using Keyboard input.
At below C# code,
1) If I remove " MessageBox.Show(State.ToString()); ", I can't escape the while loop.
2) If I add "MessageBox.Show(State.ToString());", I can escape the while loop. But, it's not perfect. Because after I see the MessageBox " false ", I must press "A" and Enter. Then Next Step, State is changed to "true". And I can see "true" > " Entered !!! " > "While Loop is Broken !!!" MessageBox.
a) What is the difference 1) and 2).
b) How can I escape while loop using keyboard input? Easily.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
namespace WF_Templete_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void bt_Start_Click(object sender, EventArgs e)
{
while (true)
{
bool State = (System.Windows.Input.Keyboard.IsKeyDown(System.Windows.Input.Key.A) == true);
// MessageBox.Show(State.ToString());
if (State)
{
MessageBox.Show("Entered !");
break;
}
}
MessageBox.Show("While Loop is Broken !!!");
}
}
}