0

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.

Jesse Myers
  • 69
  • 1
  • 8
  • where -at which line- is the exception thrown? – kennyzx Jul 30 '15 at 01:31
  • `lbPlayTime.BeginInvoke(new Action(() => { lbPlayTime.Update(); }));)` @kennyzx – Jesse Myers Jul 30 '15 at 01:34
  • Honestly, I don't see how your current posted code can give you that specific error (unless `lbPlayTime` is somehow not created on the UI thread?). As an aside though, your code seems to expect the `BeginInvoke` calls to occur in a synchronous fashion, but it won't. If you're on .NET 4.5+, consider trying out the [async-await](https://msdn.microsoft.com/en-us/library/hh191443.aspx) keywords. I think it will help you to accomplish your goal while keeping the code clean. – sstan Jul 30 '15 at 01:40
  • 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.. – Jesse Myers Jul 30 '15 at 01:43
  • @sstan i have tried that before when i was trying to update a chart on a separate thread but it didn't work for me, maybe because i don't understand how I'm supposed to use it since i've tried to adopt something i read on somebody else's post – Jesse Myers Jul 30 '15 at 01:44

0 Answers0