public void run()
{
startServer = new Thread(new ThreadStart(startUdpServer));
startServer.Start();
Thread.Sleep(1000);
} 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));
}
}
} }
{
startServer = new Thread(new ThreadStart(startUdpServer));
startServer.Start();
Thread.Sleep(1000);
} 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));
}
}
} }
解决方案 »
- linq: 请问List<Person> a 按照年龄排序后,为啥结果为空,本来不为空的!!
- foreach循环的问题
- Webbrowser控件,调试时出现当前页面的脚本发生错误(64位的win7 如何打开32位的注册表)
- 求一个好的用C#解析网页的方法
- 计算器的键盘输入问题
- C#如何打印的问题?高手进来啊?
- 谁有Sony VGN-S46C的安装盘?
- 编写一个纸牌游戏,要求可以局域网内对战(一人建个主机,其他人加入,一起准备,然后玩),但是没有类似编程经验,语言是C#或者java。急需。
- DataGrid中的信息列表如何自动删除????
- 我用vs2003.net写C#程序时,添加到窗体上的控件会莫名其妙地丢失,郁闷!各位碰到过没有?
- webform中能不能制作自己的控件???
- 如何电击按钮关闭窗体但是不弹出对话框
other than thread it was create on.该如何处理? 用delegate么要?那位好心 帮下 谢谢
详细的自己去看MSDN...
还是 StartUdpServer 里
两种方法可以解决:
1.构造函数里加CheckForIllegalCrossThreadCalls = false;此时就和2003是一样的了,但可能会有一些问题,就像你遇到的,界面刷新可能有点问题,建议使用下面一种解决方法.
2.用BeginInvoke+delegate,我给你摘点代码,楼主自己研究一下,对你来说应该不难.
private void ThreadFun() {
//Create invoke method by specific function MethodInvoker mi = new MethodInvoker( this.InvokeFun );
for( int i = 0; i < 100; i++ )
{
this.BeginInvoke( mi );//用于界面更新
Thread.Sleep( 100 );
}
}
Invoke函数入口:
private void InvokeFun() {
if( prgBar.Value < 100 )
prgBar.Value = prgBar.Value + 1;
}
MyInvoke mi = new MyInvoke(this.MyInvokeMethold);
this.BeginInvoke(mi, new object[] { item});//更新界面程序段入口
private void MyInvokeMethold(ListViewItem item)
{
//进行添加操作
....
}没有测试,楼主不妨一试。
this.BeginInvoke(mi, new object[] { item}); object 后就一 参数啊??
而最终 是以DLL文件 加入 BizTalk2006中的以前没搞过 接触C#也不长 见笑了
delegate void SetText2CallBack(string text, object sender, int type);
private void SetText2(string text, object sender, int type)
{
TextBox tb = sender as TextBox;
if (tb == null)
return;
if (tb.InvokeRequired)
{
SetText2CallBack d = new SetText2CallBack(SetText2);
this.Invoke(d, new object[] { text, sender, type });
}
else
{
if (type == 1)
{
tb.Text = text;
}
}
}在子线程中要向某个控件(例如文本框)写数据的时候调用:
注 text:要写的字符串 sender:要写的控件 type:写得方式,如覆盖或追加等
SetText2(string text, object sender, int type)
item.SubItems.Add(st);
item.SubItems.Add("1");
不就两列吗?你的代码中怎么会出现"subItems[2]"呢?
是我理解有误,还是你写的有误?
item.SubItems.Add(st);
item.SubItems.Add("1");
不就两列吗?你的代码中怎么会出现"subItems[2]"呢?
是我理解有误,还是你写的有误?
没写错啊 其实显示的有三列内容 ID TAG TIMES"subItems[2]" 是次数啊, 意思是: 如果读到卡和每一行的卡号比较
比较后确定相同 "subItems[2]" 上就加"1" 次不同 就另起一行的
下面是 初始代码 int ttt;
int kkk;
int ddd;
listView1.View = View.Details;
// Add a column with width 20 and left alignment.
listView1.Columns.Add("ID", 30, HorizontalAlignment.Left);
listView1.Columns.Add("Tag", 120, HorizontalAlignment.Left);
listView1.Columns.Add("Times", 50, HorizontalAlignment.Left);
listView1.Columns.Add("Memo", 50, HorizontalAlignment.Left);
如果要在线程中为控件赋值请在构造函数中加上CheckForIllegalCrossThreadCalls = false;加哪里?加在类的构造函数中啊,晕倒!