The below code works fine, does not lock the UI, but removing thread.sleep does. Just trying to understand why. Thanks
public partial class MainFrm : Form
{
public readonly SynchronizationContext UiContext = null;
public MainFrm()
{
InitializeComponent();
UiContext = SynchronizationContext.Current;
}
private async void Start_Click(object sender, EventArgs e)
{
await Task.Run(() =>
{
for (var i = 0; i <= 5000000; i++)
{
Thread.Sleep(1); //anything but 0 works, having no sleep also fails, why?
UiContext.Post(o => { UItextBox.Text = $@"The integer is: {(int) o}"; }, i);
}
});
}
}