This is not a duplicate of Thread.sleep() in while loop doesn't work properly? Because that one the accepted answer is that he mixed up milliseconds and seconds.
Note- somebody suggested that Keep UI thread responsive when running long task in windows forms would solve my question. The example in that question is very complicated in that it has lots of things that aren't relevant and answers cater to that and include all that complexity. It's much harder to understand that question and answer. My question is far simpler and hence the answer is much simpler. The accepted answer here has no token.ThrowIfCancellationRequested();
It's very hard to adapt the complex answer at that link, to my simple question. I would certainly not accept the accepted answer to that question as an answer to mine. Hans has actually given a one line answer. I don't think a one line answer applies to that question. So my question is different to that one.
I have a form with a button and a label
I have this code when the button is clicked
A button click calls a function that does this
int a, b;
int i=0;
while(i<5)
{
i += 1;
Thread.Sleep(1000);
label1.Text += "a";
}
MessageBox.Show("done");
I'd 'expect'/want, that that should write each of 5 'a's, pausing for a second between each one.
But it doesn't, it buffers them. it waits 5 seconds then writes all the 'a's in one go and prints 'done'.
Why is it buffering?
I don't want to use a timer because I want it to be sequential. I don't want 'done' to print till that timed task is done.
How can get the code I have to work as I want?
There is an accepted answer on this question C# - How to pause application until timer is finished? I don't quite understand it, I would test it but but I can't even see how to test it, because I can't really see where i'd put the code from the accepted answer, how i'd adapt it https://i.stack.imgur.com/FUB1G.png It seems maybe that accepted answer has subclassed timer or something, and that doesn't seem necessary to my problem/question.