There is a program in which there is a stream, something is executed there, and then you need to put it all into a list. Nothing happens. Long search found such a solution

this.Invoke((MethodInvoker)delegate() { listBox1.Items.Clear(); listBox1.Items.AddRange(names); }); 

but this option is also not suitable, an exception occurs

"The exception type" System.ArgumentNullException "occurred in System.Windows.Forms.dll, but was not processed in the user code"

If you click continue, then an Exception is triggered.

value cannot be null. parameter name: item

Framework 4.5. I do not understand what's wrong.

ps in sharpe recently, mainly deal with java

Reported as a duplicate by members Pavel Mayorov , aleksandr barakin , Ivan Pshenitsyn , cheops , Denis Aug 16 '16 at 6:05 .

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 .

2 answers 2

Here I have an example of adding a line to ListView, but I think you will figure out how to edit the code for your purposes.

 private delegate void UpdateListView(String message); private void btnCreateThread_Click(object sender, EventArgs e) { Thread thread = new Thread(threadFunction); thread.Start(); } private void threadFunction() { UpdateList add = addListBoxItem; String msg = "bla-bla-bla"; Invoke(add, new object[] { msg }); } void addListBoxItem(String message) { listBox.Items.Add(message); } 
  • it is not clear what is happening in your invoke: D - Maxim Frolov
  • In theory, calling the addListBoxItem method with passing the message parameter :) In general, the code was written in the second year of uni, and now I’m lazy to return to it, so it may well be that there is complete nonsense. But he worked: D - Sleepy Panda
  • And how does this help deal with ArgumentNullException? - Pavel Mayorov

To complete the picture:

 public partial class FormMain : Form { public static FormMain EventSend; public FormMain() { InitializeComponent(); EventSend = this; } public void AppendMessage(string value) { if (InvokeRequired) { this.Invoke(new Action<string>(AppendMessage), new object[] { value }); return; } listBox1.Items.Add(value); } private void button1_Click(object sender, EventArgs e) { Thread thread = new Thread(ForThread.DoIt); thread.Start(); } } class ForThread { private static void SendMessage(string value) { FormMain.EventSend.AppendMessage(value); } public static void DoIt() { // тут появляется магия вашего потока и генерируются names, // а для примера: for (int i = 1; i <= 5; i++) { string names = i.ToString(); // И на всякий случай if (names != null) { SendMessage(names); } } } } 

Update:

Add methods:

 private void AppendMessage(string value) { if (InvokeRequired) { this.Invoke(new Action<string>(AppendMessage), new object[] { value }); return; } listBox1.Items.Add(value); } private static void SendMessage(string value) { AppendMessage(value); } 

And instead

 this.Invoke((MethodInvoker)delegate() { listBox1.Items.Clear(); listBox1.Items.Add(names); }) 

This

  foreach (string name in names) { if (name =! null) SendMessage(name); } 

And listBox1.Items.Clear (); before running the stream in auth ();

  • And how does this help deal with ArgumentNullException? - Pavel Mayorov
  • And the fact that, in a question, it is not clear where and how the "names" is taken and ArgumentNullException can cause anything, and in the given example the whole chain of events is clearly visible - what happens in the stream and how to pass the value to the form. - DartAlex
  • pastebin.com/bM2bBhx8 here is the code - Maxim Frolov
  • Updated code. And check just in case there are no null values ​​in the names array. - DartAlex
  • checked with breakpoints, if (name! = null) does not work, then all names are empty - Maxim Frolov