so I have this really frustrating problem every time I try to create a new thread for a function that has do perform some operations involving the UI controls.
all I need this for is to be able to break out of this loop when a button in the GUI is pressed but that doesn't happen because the button is not responsive until this method is finished executing. is it possible to maybe just run that button on a separate thread and make it change a volatile variable or something like that (a simple elegant solution) instead of having to modify every single thing in this method.
I've read a most of the previous question and have tried some stuff but it never seems to work. I don't understand why something so intuitive is so hard to accomplish.
Here is the function I'm referring to:
private void pbPlay_MouseDown(object sender, MouseEventArgs e)
{
pbPlay.Image = Resources.play_d;
//playBack();
T3 = new Thread(playBack); T3.Start();
}
private void playBack()
{
var frameTime = new DateTime(); var frameTime_ = new DateTime();
int playTime = 0; ;
trPlay.BeginInvoke(new Action(() =>
{
playTime = trPlay.Value;
}));
playGoStop = true;
lbPlayTime.BeginInvoke(new Action(() =>
{
lbPlayTime.Text = (playTime * numDtStepSize.Value).ToString();
}));
while (playTime < trPlay.Maximum + 1)
{
frameTime = DateTime.UtcNow;
if ((frameTime - frameTime_).TotalMilliseconds > (double)(1000 / numFps.Value))
{
//systemUpdate(playTime);
//trPlay.Value = playTime;
//trPlay.Update();
lbPlayTime.BeginInvoke(new Action(() =>
{
lbPlayTime.Update();
}));
frameTime_ = frameTime;
playTime++;
}
if (!playGoStop) break;
}
}
So the BeginInvoke I've added after reading about it in some other threads but it doesn't work and it keeps crashing. there are a bunch of GUI operations taking place in this function both get and set, I hope there is a way of handling this without having to go through so many hoops for every single operation.
In this particular case I'm running this function on a separate thread because I need my UI to stay responsive while the function executes, and I need to be able to change the variable playGoStop
when I click on a pause button while this code is executing.