I need two methods to be executing in parallel. Work()
changes data inside infinite loop. Represent
puts the changed data in the TextBox
inside the infinite loop as well.
Log log = Log.GetInstance; //singleton
private void Work()
{
while (true) { //changing log }
}
private async void Represent()
{
await Task.Run(() =>
{
while (true)
{
String str = String.Empty;
//generating str from log
textBox.Text = str;
}
});
}
private async void button_Click(object sender, EventArgs e)
{
await Task.Run(() => Work());
}
public MainForm()
{
Represent();
}
The problem is that textBox.Text = str;
generates an error "invalid operation in multiple threads: attempt to access the control "textBox" from a thread in which it was created"
. How to solve that problem? Thanks in advance.
P.S. Suggested method here for .NET 4.5 doesn't work because of the infinite loop.