-1

Using Winform with C# .Net.

I have a small issue which I thought was easy. When I run a function continuously the UI locked up so I decided to put the function on a background thread which I though would free up the UI. I wanted to free the UI so I could change the value on the scroll bar.

I did this add a button to start the thread I believe in the background

private void btn_stepper_start_Click(object sender, EventArgs e)
        {
            
           var thread = new Thread(() =>
           {
               while (true)
               {
                   RunStepper();
               }

           });
            
            thread.IsBackground = true;
            thread.Start();

        }

Run the function. I use Invoke to stop cross threading but you may know a better way

private void RunStepper()
        {
              Invoke(new Action(() =>
                {    
                    val1 = trkbr_stepper.Value;

                    if (val1 > 540)
                    {    
                      //do some work here
                       // I leave this out to make code smaller for you
                    }
                 }));
           }

I found I had to have this event as without it the function above did not seem to run.

private void trkbr_stepper_Scroll(object sender, EventArgs e)
        {
        
        }

My problem is the UI is still locked so I cannot scroll the taskbar. What have I missed or done wrong please?

user3884423
  • 556
  • 1
  • 5
  • 20
  • 5
    problem is: from your other thread, you're hammering your UI with updates _nonstop_, as fast as it can go. the UI just doesn't have any _time_ between two executions of `RunStepper` to do its thing. – Franz Gleichmann Jun 29 '21 at 08:48
  • 3
    `Invoke()` is "Get me back running on the UI thread". If the only thing your thread does is immediately `Invoke`, you've just performed a really expensive no-op. – Damien_The_Unbeliever Jun 29 '21 at 08:52
  • Use backgroundworker – Mansur Kurtov Jun 29 '21 at 11:22
  • @Franz: while excessively-frequent calls to `BeginInvoke()` can cause the UI to become unresponsive, that's not what's going on here. There's only one cross-thread invocation in this example. _(Also, `Invoke()` doesn't usually lead to that even when there are multiple invocations, because it's synchronous and so won't fill up the message queue...it will lead to a different problem, which is abysmal performance of the background task, because it spends nearly all of its time waiting on UI updates instead of doing real work)._ – Peter Duniho Jun 29 '21 at 16:45

1 Answers1

-1

Thanks for the help, I learnt something here about Invoke. From comments by @damien_the_unbeliever I modified the code to remove the Invoke and now it is running OK.I can change the settings via the scrollbar.

private void RunStepper()
        {                  
                    if (val1 > 540)
                    {    
                      //do some work here
                       // I leave this out to make code smaller for you
                    }
          }

Make val1 global I know that is not good and I will change later but for now this works.

private void trkbr_stepper_Scroll(object sender, EventArgs e)
        {
          val1 = trkbr_stepper.Value;
        }
user3884423
  • 556
  • 1
  • 5
  • 20