This question has already been answered:

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"); // ошибка } 

Reported as a duplicate by PashaPash member c # Mar 20 '18 at 15:35 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • Win Forms, WPF? - Anton Shakalo
  • WinForms completely forgot to specify - viton-zizu

2 answers 2

 public static void addComment(string comment) { _loglistbox.Dispatcher.BeginInvoke(new Action(() => { _loglistbox.Items.Add(comment); _loglistbox.SelectedIndex = _loglistbox.Items.Count - 1; _loglistbox.SelectedIndex = -1; })); } 
  • Where is the reference to the static class? How will it work? - Roman Ieromenko
  • one
    @RomanIeromenko did not understand the question ... To which static class and why? - yolosora
  • @RomanIeromenko is a valid solution for WPF (there all Dispatcher property controls). You have a Winforms solution. - PashaPash
  • Thanks, I will know - Roman Ieromenko

I have another solution without the class of the closed Dispatcher, first I would add a property to the _loglistbox:

  static class Log { static private ListBox _loglistbox; **static public ListBox Loglistbox { get { return _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; } } 

Well, the implementation itself is, by the way, missed where the internal static variable of the ListBox class comes from

  private void writeDataToBase() { BeginInvoke((MethodInvoker) (() => { **Log.initialize(new ListBox());** Log.addComment("Bla Bla Bla"); } )); } 
  • Thanks for the help, I decided in this way: if (_loglistbox.InvokeRequired) _loglistbox.Invoke(new Action<string>((s) => { _loglistbox.Items.Add(s); }), comment); - viton-zizu