So I have code something like that
private void doSmth()
{
str = makeStr();
}
private void button_Click(object sender, EventArgs e)
{
Task task = new Task(doSmth);
task.Start();
task.Wait();
textBox.Text = str;
}
It's freezing and I know why this is happening because of Wait()
. I was trying to use ContinueWith()
like this
task.ContinueWith((t) => {textBox.Text = str;});
But it doesn't work throwing an InvalidOperationException
:
The calling thread cannot access this object because a different thread owns it
How can I fix this? Maybe I should use completely another approaches to implement what I want to. Thanks.