I am writing in C# and using .net framework 3.5. I am running through multiple loops that on each iteration build the UI and then wait for user feedback (waiting for a Pass or Fail button click). Here is what I am looking to do:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// add both button handlers to function btn_Click
for(int test = 0; test < 5; test++)
for(int variation = 0; variation < 4; variation++)
for(int subtest = 0; subtest < 3; subtest++)
{
// call function to update GUI
// may need to do stuff while at this state
// wait for user to click pass/fail button
}
}
private void btn_Click(object sender, EventArgs e)
{
// pass button pressed, inform for loop to iterate to next test
// if fail button, then stop tests
}
}
The wait inside the for loop is what gets me. Since this is single threaded I was running into issues with Sleep and do not like the idea of putting the check for a button press inside a while loop with a global variable. I tried AutoResetEvent to wait for the button click but that gave me issues too. What is the best way to handle this?
I did write the code in a way that I could try to implement it but do not think it is a very elegant solution:
public partial class Form1 : Form
{
int test;
int variation;
int subtest;
public Form1()
{
InitializeComponent();
test = 0;
variation = 0;
subtest = 0;
// update GUI
btnUpdate.Text = "Hello # " + test.ToString() + "." + variation.ToString() + "." + subtest.ToString();
}
private void btnUpdate_Click(object sender, EventArgs e)
{
subtest++;
if (test >= 5)
if (variation >= 4)
if (subtest >= 3)
{
// done case
Console.WriteLine("Tests are complete");
btnUpdate.Visible = false;
}
if (subtest > 3)
{
subtest = 0;
variation++;
}
if (variation > 4)
{
variation = 0;
test++;
}
Console.WriteLine("I was clicked");
// update button
btnUpdate.Text = "Hello # " + test.ToString() + "." + variation.ToString() + "." + subtest.ToString();
}
}
Thanks in advance!
Just use a separate thread to UpdateUI and block it while button not pressed: