This question has already been answered:
- Work with controls from background thread 2 responses
How to run a method from a UI stream? I have a static class Log for outputting messages to the log from all parts of the program. Looks like that:
static class Log { static private ListBox _loglistbox; public static void initialize(ListBox loglistbox) { _loglistbox = loglistbox; } public static void addComment(string comment) { _loglistbox.Items.Add(comment); _loglistbox.SelectedIndex = _loglistbox.Items.Count - 1; _loglistbox.SelectedIndex = -1; } } At the start, I transfer there to which listbox to write and it regularly performs its duties. I started translating my main method into a stream, so that the interface would not be slowed down during execution. From the thread, when I access the Log class, I receive an error of addressing from another thread, please tell me how to correctly access the Log class from another thread so that it can display a message in the listbox?
private void button2_Click(object sender, EventArgs e) { Thread writeDataThread = new Thread(writeDataToBase); writeDataThread.Start(); } private void writeDataToBase() { Log.addComment("Start"); // ошибка }