Hi,
I have a backgroundthread where I in this moment is using:
while (runsomeCode == false)
{
Thread.Sleep(1);
}
to wait for "runsomeCode" will turn to: 'true' from another thread, which is button9_Click in this example.
However, using Thread.Sleep(1) doesn´t seem to be a very effective way.
I wonder if there could be any other method to wait inside the "while(true)" loop, for the variable "runsomeCode" to turn 'true'. When that happens, the code below will run once and the procedure starts from the beginning as seen in the code below.
bool runsomeCode = false; bool backgroundthreadHasStarted = false;
private void button9_Click(object sender, EventArgs e)
{
//Start the background worker thread
if (backgroundthreadHasStarted == false)
{
Thread thread = new Thread(backgroundthread);
thread.Start();
backgroundthreadHasStarted = true;
}
runsomeCode = true;
}
public void backgroundthread()
{
while (true)
{
while (runsomeCode == false)
{
Thread.Sleep(1);
}
runsomeCode = false; //Reset
this.Invoke((MethodInvoker)delegate
{
MessageBox.Show("Some code will run here!");
});
}
}