问个关于线程的 问题 顶者有分 (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.

还是这个问题 顶的有分 愁死我了
...全文
633 57 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
57 条回复
切换为时间正序
请发表友善的回复…
发表回复
色郎中 2006-10-30
  • 打赏
  • 举报
回复
to:liujia

我把真实的卡号去掉了, ST="aaaa" 用这个做为卡号
没大明白,能否说得再详细点...

因为你没真实的设备 读不到卡 ,ST="aaaa" 这个算虚拟的卡号
如过你把代码建立个简单的工程 就可以在listview上看 aaaa 这个虚拟卡号不断增加
不用真实的去连接设备读卡了
色郎中 2006-10-30
  • 打赏
  • 举报
回复
谢谢你 AWEN

TIMER 的我试过了是可以的 呵呵 可以定时刷新的
同时也不存在 这个问题 就是交叉线程

按MS专家的意思 一定要用线程
不行去北京再说了
awen177 2006-10-30
  • 打赏
  • 举报
回复
设置个timer试试
色郎中 2006-10-30
  • 打赏
  • 举报
回复
噢 了解
那和MCU程序不一样啊

while(ture)
{
run();
}

就是这样个大循环的啊 程序一直工作

那C# 有什么办法让这个线程 一直工作 或 间隔100ms工作一次?
liujia_0421 2006-10-30
  • 打赏
  • 举报
回复
private void btnTest_Click(object sender, EventArgs e)
{
while(ture)
{
run();
}
}
让这个startUdpServer()的线程一直运行 能成么?


这是死循环,程序当然会死掉啊....
色郎中 2006-10-30
  • 打赏
  • 举报
回复
TO LIUJIA

输出为:
ID   Tag   Times  Memo
1   aaaa 10

你的输出为这个结果的 代码中 startUdpServer()在这个函数里用了 10次的循环啊

输出的效果是对的,但方法不是想要的
我想的是把下面的BUTTON里代码


private void btnTest_Click(object sender, EventArgs e)
{
run();
}

改为
private void btnTest_Click(object sender, EventArgs e)
{
while(ture)
{
run();
}
}
让这个startUdpServer()的线程一直运行 能成么?

我改了 但程序死了
色郎中 2006-10-30
  • 打赏
  • 举报
回复
输出为:
ID   Tag   Times  Memo
1   aaaa 10

我要的是这种 谢谢你兄弟

留个MSN 吧 我觉得以后这个BizTalk2006 这个RFID中间件
开发中还会有不少问题,希望能多指教
liujia_0421 2006-10-30
  • 打赏
  • 举报
回复
不知是否是楼主说的意思....
liujia_0421 2006-10-30
  • 打赏
  • 举报
回复
稍微修改一下:
将string st = "aaaa";修改为:
string st = "aaaa"+iii.ToString ();

即:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace TestListView
{
public partial class TestListView : Form
{
private Thread startServer;
public delegate void MyInvoke1(int id);//用于对相同的项增加次数
public delegate void MyInvoke2(int id, string st);//用于增加新的项
int ttt;
int kkk;
int ddd;
string subItemText;


private void CreateNewItem(int id, string st)
{
//添加一个新的项
ListViewItem item = new ListViewItem(id.ToString());
item.SubItems.Add(st);
item.SubItems.Add("1");
listView1.Items.Add(item);
int aaa = listView1.Items.Count;
}

private void UpdateItemTimes(int id)
{
//将出现相同的项次数增1
listView1.Items[id].SubItems[2].Text = Convert.ToString((Convert.ToInt32(listView1.Items[ttt].SubItems[2].Text) + 1));
}

private void GetSubItemText(int index)
{
//得到子项的值
subItemText=Convert.ToString(listView1.Items[index].SubItems[1].Text);
}


public TestListView()
{
InitializeComponent();
ttt=0;
kkk=0;
ddd=1;
}

public void run()
{
startServer = new Thread(new ThreadStart(startUdpServer));
startServer.Start();
}

private void btnTest_Click(object sender, EventArgs e)
{
run();
}
private void startUdpServer()
{
for (int iii = 0; iii < 10; iii++)
{
//ListViewItem item;
//string str = textBox1.Text;
string st = "aaaa"+iii.ToString ();
//ddd = 1;

if (listView1.Items.Count < 1)// 如果 listview上的记录为空 则添加一个记录;
{
//改的地方1
//item = new ListViewItem(ddd.ToString());
//item.SubItems.Add(st);
//item.SubItems.Add("1");
//listView1.Items.Add(item);
//int aaa = listView1.Items.Count;
MyInvoke2 mi = new MyInvoke2(CreateNewItem);
this.BeginInvoke(mi, new object[] { ddd, st });
}
else
{
for (int m = 0; m < listView1.Items.Count; m++)
{
MyInvoke1 mi = new MyInvoke1(GetSubItemText);
//string str = Convert.ToString ( );
this.BeginInvoke(mi, new object[] { m });
if (st == subItemText )
{

//如果 新的内容 str,和 listview上的记录行 相同 则做
ttt = m;
kkk = kkk + 1;
}
else //如果 listview 上 tag 列的内容和 str 不相等
{
kkk = 0;
}
}

if (kkk == 0) //如果没有相同的记录则做如下操作,添加新的一条记录;
{
ddd = ddd + 1;
//改的地方2
//item = new ListViewItem(ddd.ToString());
//item.SubItems.Add(st);
//item.SubItems.Add("1");
//listView1.Items.Add(item);
MyInvoke2 mi = new MyInvoke2(CreateNewItem);
this.BeginInvoke(mi, new object[] { ddd, st });
}
else //若记录相等 则给listview 上的 times 列 的加“1”;
{
//改的地方3
//listView1.Items[ttt].SubItems[2].Text = Convert.ToString((Convert.ToInt32(listView1.Items[ttt].SubItems[2].Text) + 1));
MyInvoke1 mi = new MyInvoke1(UpdateItemTimes);
this.BeginInvoke(mi, new object[] { ttt });
}
}
}

}

}
}

输出如下:
ID   Tag   Times   Memo
1    aaaa0 1
2    aaaa1 1
3    aaaa2 1
4    aaaa3 1
5    aaaa4 1
6    aaaa5 1
7    aaaa6 1
8    aaaa7 1
9    aaaa8 1
10   aaaa9 1
liujia_0421 2006-10-30
  • 打赏
  • 举报
回复
测试程序如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace TestListView
{
public partial class TestListView : Form
{
private Thread startServer;
public delegate void MyInvoke1(int id);//用于对相同的项增加次数
public delegate void MyInvoke2(int id, string st);//用于增加新的项
int ttt;
int kkk;
int ddd;
string subItemText;


private void CreateNewItem(int id, string st)
{
//添加一个新的项
ListViewItem item = new ListViewItem(id.ToString());
item.SubItems.Add(st);
item.SubItems.Add("1");
listView1.Items.Add(item);
int aaa = listView1.Items.Count;
}

private void UpdateItemTimes(int id)
{
//将出现相同的项次数增1
listView1.Items[id].SubItems[2].Text = Convert.ToString((Convert.ToInt32(listView1.Items[ttt].SubItems[2].Text) + 1));
}

private void GetSubItemText(int index)
{
//得到子项的值
subItemText=Convert.ToString(listView1.Items[index].SubItems[1].Text);
}


public TestListView()
{
InitializeComponent();
ttt=0;
kkk=0;
ddd=1;
}

public void run()
{
startServer = new Thread(new ThreadStart(startUdpServer));
startServer.Start();
}

private void btnTest_Click(object sender, EventArgs e)
{
run();
}
private void startUdpServer()
{
for (int iii = 0; iii < 10; iii++)
{
//ListViewItem item;
//string str = textBox1.Text;
string st = "aaaa";
//ddd = 1;

if (listView1.Items.Count < 1)// 如果 listview上的记录为空 则添加一个记录;
{
//改的地方1
//item = new ListViewItem(ddd.ToString());
//item.SubItems.Add(st);
//item.SubItems.Add("1");
//listView1.Items.Add(item);
//int aaa = listView1.Items.Count;
MyInvoke2 mi = new MyInvoke2(CreateNewItem);
this.BeginInvoke(mi, new object[] { ddd, st });
}
else
{
for (int m = 0; m < listView1.Items.Count; m++)
{
MyInvoke1 mi = new MyInvoke1(GetSubItemText);
//string str = Convert.ToString ( );
this.BeginInvoke(mi, new object[] { m });
if (st == subItemText )
{

//如果 新的内容 str,和 listview上的记录行 相同 则做
ttt = m;
kkk = kkk + 1;
}
else //如果 listview 上 tag 列的内容和 str 不相等
{
kkk = 0;
}
}

if (kkk == 0) //如果没有相同的记录则做如下操作,添加新的一条记录;
{
ddd = ddd + 1;
//改的地方2
//item = new ListViewItem(ddd.ToString());
//item.SubItems.Add(st);
//item.SubItems.Add("1");
//listView1.Items.Add(item);
MyInvoke2 mi = new MyInvoke2(CreateNewItem);
this.BeginInvoke(mi, new object[] { ddd, st });
}
else //若记录相等 则给listview 上的 times 列 的加“1”;
{
//改的地方3
//listView1.Items[ttt].SubItems[2].Text = Convert.ToString((Convert.ToInt32(listView1.Items[ttt].SubItems[2].Text) + 1));
MyInvoke1 mi = new MyInvoke1(UpdateItemTimes);
this.BeginInvoke(mi, new object[] { ttt });
}
}
}

}

}
}

输出为:
ID   Tag   Times  Memo
1   aaaa 10

色郎中 2006-10-30
  • 打赏
  • 举报
回复
CheckForIllegalCrossThreadCalls = false;

加这个在构造函数里
private void button7_Click(object sender, EventArgs e)
{
run();
}

run() 这样写能显示 一次

但 这样写
private void button7_Click(object sender, EventArgs e)
{
while
{
run();
}
}

就啥也没有了
破碎的脸 2006-10-29
  • 打赏
  • 举报
回复
这是一个网吧管理系统。
首先在SQL里用getdate()得到当前登陆时间,然后以预存金额与所定义的时段收费(时段收费是在另一张表中,有二十四个字段,从零到二十三,里面记录了每个小时的收费标准)进行减运算,逐一相减,比如20块,现在时间为晚上八点,那么就是20-八点收费标准-九点收费标准以此类推,直到小于或等于1,然后用余数与下一个时段收费标准进行相除,得出所能上网的时长,精确到分,然后与当前登陆时间进行相加,然后得出预计下机时间。
第一个做出来的送分一百,只能在SQL里完成,不能使用其它代码,当然如果有JAVA代码或者C#代码作参考,会补送一些补偿分。请高手帮忙!

顶顺便说一下我的问题,贴子在C#专区。。。。一百分,在线等。。。
股神 2006-10-29
  • 打赏
  • 举报
回复
up
股神 2006-10-28
  • 打赏
  • 举报
回复
apple_tang 2006-10-28
  • 打赏
  • 举报
回复
就是就是,没前没后的,看得累人
yitian130 2006-10-28
  • 打赏
  • 举报
回复
xinxin85 2006-10-28
  • 打赏
  • 举报
回复
有分么 我顶下
pshy 2006-10-28
  • 打赏
  • 举报
回复
頂........
seemwind 2006-10-28
  • 打赏
  • 举报
回复
那么长,看的好累
leeyeefeng2004 2006-10-27
  • 打赏
  • 举报
回复
1。触发事件
2。只有脱离线程,才能控制你的listview
methodXXXX mk =new methodXXX(委托方法名)
this.invoke(委托方法名)
具体的忘记了,单词不会。。。呵呵
加载更多回复(37)

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧