问个关于线程的 问题 顶者有分 (2)
色郎中 2006-10-27 01:14:19 public void run()
{
startServer = new Thread(new ThreadStart(startUdpServer));
startServer.Start();
}
private void button7_Click(object sender, EventArgs e)
{
run();
}
startUdpServer函数内容如下:
int i, j;
byte[] receive = new byte[1200];
byte[] realdata = new byte[256];
ASCIIEncoding s = new ASCIIEncoding();
udp.Connect("192.168.7.69", 1969);
IPEndPoint ipadr = new IPEndPoint(IPAddress.Parse("192.168.7.69"), 10001);
receive = udp.Receive(ref ipadr);
if (receive.Length > 119)
{
for (i = 0; i < (int)(receive.Length / 120); i++)
{
st = "";
for (j = 0; j < 29; j++)
{
Array.Resize(ref realdata, 29);
realdata[j] = receive[i * 120 + 0x4d + j];
}
st += s.GetString(realdata);
}
ListViewItem item;
string str = textBox1.Text;
ddd = 1;
if (listView1.Items.Count < 1)// 如果 listview上的记录为空 则添加一个记录;
{
item = new ListViewItem(ddd.ToString());
item.SubItems.Add(st);
item.SubItems.Add("1");
listView1.Items.Add(item);
int aaa = listView1.Items.Count;
}
else
{
for (int m = 0; m < listView1.Items.Count; m++)
{
if (st == Convert.ToString(listView1.Items[m].SubItems[1].Text))
{
//如果 新的内容 str,和 listview上的记录行 相同 则做
ttt = m;
kkk = kkk + 1;
}
else //如果 listview 上 tag 列的内容和 str 不相等
{
kkk = 0;
}
}
if (kkk == 0) //如果没有相同的记录则做如下操作,添加新的一条记录;
{
ddd = ddd + 1;
item = new ListViewItem(ddd.ToString());
item.SubItems.Add(st);
item.SubItems.Add("1");
listView1.Items.Add(item);
}
else //若记录相等 则给listview 上的 times 列 的加“1”;
{
listView1.Items[ttt].SubItems[2].Text = Convert.ToString((Convert.ToInt32(listView1.Items[ttt].SubItems[2].Text) + 1));
}
}
}
}
问题出现在startUdpServer函数 因为listview 这个控件吧
Cross-thread operation not valid: control'listview' accessed from a thead
other than thread it was create on.
还是这个问题 顶的有分 愁死我了