D
Dave
The problem is with my listbox on the form.
I am trying to run 2 threads simulataneously an update two different listbox
controls but my functions are complaining that the listbox doesn't have an
object reference, my listboxes were created by the ide and drawn on the
form at design time.
How can I give my listbox a reference?
Thanks
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
static List<string> list = new List<string>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
new Thread(AddItems1).Start();
new Thread(AddItems2).Start();
}
static void AddItems1()
{
for (int i = 0; i < 10000; i++)
//lock (list)
list.Add("Item " + list.Count);
string[] items;
items = list.ToArray();
foreach (string s in items) Form1.listBox1.Items.Add(s);
}
static void AddItems2()
{
for (int i = 0; i < 10000; i++)
//lock (list)
list.Add("Item " + list.Count);
string[] items;
items = list.ToArray();
foreach (string s in items) Form1.listBox2.Items.Add(s);
}
}
}
I am trying to run 2 threads simulataneously an update two different listbox
controls but my functions are complaining that the listbox doesn't have an
object reference, my listboxes were created by the ide and drawn on the
form at design time.
How can I give my listbox a reference?
Thanks
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
static List<string> list = new List<string>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
new Thread(AddItems1).Start();
new Thread(AddItems2).Start();
}
static void AddItems1()
{
for (int i = 0; i < 10000; i++)
//lock (list)
list.Add("Item " + list.Count);
string[] items;
items = list.ToArray();
foreach (string s in items) Form1.listBox1.Items.Add(s);
}
static void AddItems2()
{
for (int i = 0; i < 10000; i++)
//lock (list)
list.Add("Item " + list.Count);
string[] items;
items = list.ToArray();
foreach (string s in items) Form1.listBox2.Items.Add(s);
}
}
}