问题描述
程序移植到PDA上由于没有Form.CheckForIllegalCrossThreadCalls=false;所以一直报错Control.Invoke必须用于与在独立线程上创建的控件交互,代码不完全是自己写的,所以不是非常懂,我把代码贴上,希望大哥们能帮帮小弟解决下:usingSystem;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Collections;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Threading;namespacePDA1{publicdelegatevoidEventMsg(Sessionsession);publicpartialclassForm1:Form3{TcpClim_TcpCli=newTcpCli();stringm_strServerIp;intm_iPortNum;stringmyID="";publicForm1(){InitializeComponent();Form3.ht.Add("tb_sendmsg",this.tb_sendMsg);//Form.CheckForIllegalCrossThreadCalls=false;m_TcpCli.ReceivedDatagram+=newNetEvent(_Receive_Msg);m_TcpCli.ConnectedServer+=newNetEvent(_ConnectedServer);m_TcpCli.ConnectError+=newNetEvent(_ConnectError);m_TcpCli.SendError+=newNetEvent(m_TcpCli_SendError);m_TcpCli.DisConnectedServer+=newNetEvent(m_TcpCli_DisConnectedServer);}privatestringgetdatetime(){returnSystem.DateTime.Now.ToShortDateString()+""+System.DateTime.Now.ToLongTimeString();}publicstringgetid(){Randomrnd=newRandom();inti=rnd.Next(10,99);stringid=DateTime.Now.Year.ToString()+string.Format("{0:00}",Convert.ToInt16(DateTime.Now.Month.ToString()))+string.Format("{0:00}",Convert.ToInt16(DateTime.Now.Day.ToString()))+string.Format("{0:00}",Convert.ToInt16(DateTime.Now.Hour.ToString()))+string.Format("{0:00}",Convert.ToInt16(DateTime.Now.Minute.ToString()))+string.Format("{0:00}",Convert.ToInt16(DateTime.Now.Second.ToString()))+i.ToString();returnid;}privatevoidop_con(){bt_state.BackColor=Color.Green;this.lb_state.Text="已连接!";lb_SendCliName.Text="server";}privatevoidop_coning(){bt_state.BackColor=Color.Yellow;this.lb_state.Text="连接中。。。";}privatevoidop_cls(){bt_state.BackColor=Color.Gray;this.lsb_Clients.Items.Clear();//this.t_msg.Clear();lb_SendCliName.Text="";this.lb_state.Text="未连接!";myID="";}privatevoid_Receive_Msg(objectsender,NetEventArgse){this.BeginInvoke(newEventMsg(Proc_msg),newobject[]{e.Client});}privatevoidProc_msg(Sessionse){stringstrFromServer=se.Datagram;stringlb="",sendId="",msg="";string[]strClient=strFromServer.Split('|');lb=strClient[0];sendId=strClient[1];for(inti=2;i<strClient.Length;i++){if(i>2)msg+="|"+strClient[i];elsemsg+=strClient[i];}if(lb=="msg"){UpDataLst_msg(sendId,msg);}elseif(lb=="con"){lsb_Clients.Items.Clear();if(this.myID==""){myID=sendId;this.lb_state.Text+="用户名:"+sendId;}this.t_msg.Text+=this.getdatetime()+""+sendId+"用户已登录"+"rn";string[]strIds=msg.Split('|');for(inti=0;i<strIds.Length;i++){lsb_Clients.Items.Add(strIds[i]);}}elseif(lb=="cls"){lsb_Clients.Items.Clear();this.t_msg.Text+=this.getdatetime()+""+sendId+"用户已退出"+"rn";string[]strIds=msg.Split('|');for(inti=0;i<strIds.Length;i++){lsb_Clients.Items.Add(strIds[i]);}}elseif(lb=="err"){this.t_msg.Text+=this.getdatetime()+""+"系统提示:给"+sendId+"的消息发送失败!"+"rn";}else{MessageBox.Show("消息类别错误,已断开连接!");m_TcpCli.Close();this.op_cls();}}privatevoidUpDataLst_msg(stringid,stringstr){this.t_msg.Text+=this.getdatetime()+""+"来自"+id+"的消息:rn"+str+"rn";}privatevoid_ConnectedServer(objectsender,NetEventArgse){this.op_con();}privatevoid_ConnectError(objectsender,NetEventArgse){this.op_cls();}privatevoidm_TcpCli_SendError(objectsender,NetEventArgse){MessageBox.Show("SendErrorrn发送失败,请重新连接!");try{m_TcpCli.Close();}catch(Exceptionex){MessageBox.Show(ex.Message);}this.op_cls();}voidm_TcpCli_DisConnectedServer(objectsender,NetEventArgse){m_TcpCli.Close();this.op_cls();}privatevoidbt_Connect_Click(objectsender,EventArgse){if(m_TcpCli.IsConnected){MessageBox.Show("已经建立连接!");return;}m_strServerIp=tb_ServerIp.Text.ToString().Trim();m_iPortNum=int.Parse(tb_Port.Text.ToString().Trim());try{m_TcpCli.Connect(m_strServerIp,m_iPortNum);}catch(Exceptionexx){MessageBox.Show("连接服务器:"+m_strServerIp+"失败!"+exx.Message);return;}this.op_coning();this.label_con.Text="con";}privatevoidbt_Disconnect_Click(objectsender,EventArgse){try{m_TcpCli.Close();}catch(Exceptionex){MessageBox.Show(ex.Message);}this.op_cls();this.label_con.Text="";}privatevoidbt_Send_Click(objectsender,EventArgse){if(!m_TcpCli.IsConnected){MessageBox.Show("未连接到服务器,不能发送信息!");return;}if(lb_SendCliName.Text==""){MessageBox.Show("未选中客户端对象!");return;}if(this.myID==lb_SendCliName.Text){MessageBox.Show("不能给自己发信息!");return;}this.sendToServer(this.tb_sendMsg.Text.ToString());MessageBox.Show("信息已经发送!");this.t_msg.Text+=this.tb_sendMsg.Text;}privatevoidsendToServer(stringstr){m_TcpCli.Send(str);}privatevoidlsb_Clients_SelectedIndexChanged(objectsender,EventArgse){if(lsb_Clients.Items.Count<1)return;try{stringstr=lsb_Clients.SelectedItem.ToString();string[]strClient=str.Split('');lb_SendCliName.Text=strClient[0];}catch{}}privatevoidtimer1_Tick(objectsender,EventArgse){if(this.label_con.Text=="con"&&!m_TcpCli.IsConnected&&this.lb_state.Text=="未连接!"){try{m_TcpCli.Connect(m_strServerIp,m_iPortNum);}catch{return;}this.op_coning();}}privatevoidbutton1_Click(objectsender,EventArgse){this.lb_SendCliName.Text="server";this.tb_ServerIp.Text="10.0.6.93";this.tb_Port.Text="10008";stringa="数据清空";MessageBox.Show(a.Length.ToString());this.tb_sendMsg.Text=a;}privatevoidlb_SendCliName_Click(objectsender,EventArgse){}privatevoidlsb_Clients_SelectedIndexChanged_1(objectsender,EventArgse){}protectedvoid移库电文_LinkClicked(objectsender,EventArgse){Form2fm2=newForm2();fm2.Show();}//protectedvoid上料电文_LinkClicked(objectsender,EventArgse)//{//Form5fm3=newForm5();//fm3.Show();//}privatevoidForm1_Load(objectsender,EventArgse){}privatevoidt_msg_TextChanged(objectsender,EventArgse){}privatevoidbutton2_Click(objectsender,EventArgse){Form4fm1=newForm4();fm1.Show();}}}
解决方案
解决方案二:
顶起来,希望有人帮忙解决啊
解决方案三:
楼主发错版块了?纯支持。。
解决方案四:
该回复于2012-02-01 09:03:14被版主删除