日期:2014-05-18  浏览次数:20673 次

need help:跨线程修改UI属性的几点疑问
捣鼓一天了,头都大了,麻烦各位给指点一下,老是报这个错(在):Parameter count mismatch,一直找不到根源在哪,我在BeginInvoke上也捕获不到异常。
程序逻辑是这样的,有个后台程序一直发消息给某个端口,而我要做的就是监听端口,并根据消息进行分类显示,并控制某个service。
所以我有个辅助线程是监听端口的,收到消息后触发事件,注册对象通过信息去修改UI属性(暂定为只在文本框显示文字)。
代码如下:
Form1.cs
C# code

//监听端口消息到达时事件委托
public delegate void ErrorEventHandel(string error);
//修改UI属性委托
public delegate void UpdateCurrentMsgtxt(string text);
public partial class MainFrm : Form
    {
        public MainFrm()
        {
            InitializeComponent();
            //加载控件属性
            InfoInitial();//该方法加载控件的属性,显示相关信息,并创建、开启辅助线程
        }
        //接收到消息后的处理
        void receivemsg_on_geterror(string error)
        {
            //....终止辅助线程(监听端口线程)
            TextAppend(error + "\nAuto restartting XXX service...");
            ServiceControl.restartservice(servicename, 20000);
            TextAppend("OK,service is runing.Getting config information...");
            //重启服务后,根据配置文件设定及服务状态重新加载控件的属性
            InfoInitial();
        }
        //修改X控件属性的方法
        public void TextAppend(string appendinfo)
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new UpdateCurrentMsgtxt(TextAppend));
            }
            else
            {
                this.rtxtCurrentInfo.AppendText(appendinfo);
            }
        }
    }


辅助线程类
C# code

//接收到消息后触发的事件
public event ErrorEventHandel on_geterror;
//监听端口消息
        public  void getMsgbyUDP(object listenport)
        {
            //...
            while (!shouldstop)
            {
            //....on_geterror(message);
             }
        }



------解决方案--------------------
要传参,类似
 this.BeginInvoke(new UpdateCurrentMsgtxt(TextAppend), appendinfo);