How do I write to a textbox from 3 threads?

I have 3 threads that do different jobs and I need to collect the logs about them and write it to a text box. So I have:

public delegate void Notify(string message);

Each of the 3 classes of these threads has:

public event Data.Notify Notify;

(Data is just a static class where I keep general information for my app). In the method Work() that is in all of these 3 classes I use:

Notify?.Invoke("message");

In the message I have information if the job is started, if it was successful or not. In the class of the Form I got:

Data.workingThread.Notify += Thread_Notify;
Data.readingThread.Notify += Thread_Notify;
Data.writingThread.Notify += Thread_Notify;
Data.writingThread.Start();
Data.workingThread.Start();
Data.readingThread.Start();

Where Thread_Notify is:

private void Thread_Notify(string message)
{
    tbLogs.Text += message;
}

When I run the code, It throws System.InvalidOperationException as I try to access 'tbLogs' not from a thread where it was created. I tried async/await for this method and lock(tbLogs) but these didn't work for me



Read more here: https://stackoverflow.com/questions/64396693/how-do-i-write-to-a-textbox-from-3-threads

Content Attribution

This content was originally published by user13353974 at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: