private void UpdateListBox(string item) { if (InvokeRequired) { Invoke(new Action(UpdateListBox), item); return; } listBox1.Items.Add(item); }
private void ClearListBox() { if (InvokeRequired) { Invoke(new Action(ClearListBox)); return; } listBox1.Items.Clear(); }In this example, the ClearListBox method is called to remove all items from the listbox control. The InvokeRequired property is used to determine if the control was created on a different thread. If it was, the Invoke method is used to execute the method on the thread that created the control. The System.Windows.Forms ListBox Invoke method is part of the .NET Framework class library.