The following code makes UI thread hanging.
private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(Func);
t.Start();
}
private void Func()
{
this.Invoke((Action)(() =>
{
while (true);
}));
}
I’d like to have Func() invoked in a different working thread without any UI thread freezing every time I click the button.
What would be the best workaround?
With your code,
while(true)is running on UI thread, that is the reason which blocks your UI.Put
while(true)out ofInvokemethod, so whenver you want to change UI, put the block of code changing UI insideInvoke: