Q:

Display text inside loop in C# windows application

I have a loop in my windows form
inside the loop i should display the customer number that been processed inside the loop
but the textbox that shoud have the customer number freezes during the loop and none got displayed untill loop finishes.
any ides how to resolve this?

A:

Try this:
Full reference is System.Threading.Tasks.Task, or for a using, System.Threading.Tasks
Task.Factory.StartNew(() =>
{
     for (int i = 0; i < 10; i++)
     {
          // Any GUI control which you want to use within thread,
          // you need Invoke using GUI thread. I have declare a method below for this
          //Now use this method as
          ExecuteSecure(() => textBox1.Text = "Customer Id " + i);
          //... other code etc.
          Thread.Sleep(1000);
     }
});

//---
private void ExecuteSecure(Action action)
{
    if (InvokeRequired)
    {
        Invoke(new MethodInvoker(() => action()));
    }
    else
    {
        action();
    }
}

or Use a BackgroundWorker if you want your UI to be updated in real time

