本帖最后由 tnight 于 2012-10-07 19:17:30 编辑

解决方案 »

  1.   

    ThreadStart会不停的弹出一对数值,这对数值要交给ThreadFunc来处理刚学c#,委托只用过这样的: //委托
    private delegate void TextBoxInvoke(TextBox textbox, string str); //
    //设置TextBox.Text
    //
    private void SetText(TextBox textbox, string str)
    {
    try
    {
    if (textbox.InvokeRequired)
    {
    TextBoxInvoke myinvoke = new TextBoxInvoke(SetText);
    this.Invoke(myinvoke, new object[] { textbox, str });
    }
    else
    {
    textbox.Text = str;
    }
    }
    catch (System.Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }
    }具体怎么做还是不大理解
      

  2.   

    代码是这样的: private void StartGetECode()
    {
    GetCodeThread = new Thread(new ThreadStart(this.GetECodeFunc));
    GetCodeThread.IsBackground = true; //设置为后台线程
    GetCodeThread.Name = "GetCodeThread";
    GetCodeThread.Start(); //启动线程
    }
    //线程函数
    private void GetECodeFunc()
    {
    while (ECode.GetListenState())
    {
    Thread.Sleep(100);
    KeyValuePair<string, string> KvpCode = ECode.GetECode();
    if (KvpCode.Key != string.Empty) //如果返回的内容非空
    {
    ProcessingCode(KvpCode);
    }
    }
    } //方法内容不固定
    private void ProcessingCode(KeyValuePair<string, string> KvpCode)
    {
    if (KvpCode.Key == textBox11.Text)
    {
    SetText(textBox13, KvpCode.Value);
    }
    else if (KvpCode.Key == textBox21.Text)
    {
    SetText(textBox23, KvpCode.Value);
    }
    else if (KvpCode.Key == textBox31.Text)
    {
    SetText(textBox33, KvpCode.Value);
    }
    else if (KvpCode.Key == textBox41.Text)
    {
    SetText(textBox43, KvpCode.Value);
    }
    }想把StartGetECode()、GetECodeFunc()都扔到DLL中,然后回调ProcessingCode()
      

  3.   

    StartGetECode()、GetECodeFunc()接收一个委托参数即可
      

  4.   

    没人给个简单的步骤吗。。查了很多资料,都没这方面的
    委托怎么用? 是写在DLL中还是写在调用DLL的程序中?书上也没见描述这方面的内容
      

  5.   

    这部分内容写在DLL的一个类中: private void StartGetECode()
            {
                GetCodeThread = new Thread(new ThreadStart(this.GetECodeFunc));
                GetCodeThread.IsBackground = true;    //设置为后台线程
                GetCodeThread.Name = "GetCodeThread";
                GetCodeThread.Start();        //启动线程
            }
            //线程函数
            private void GetECodeFunc()
            {
                while (ECode.GetListenState())
                {
                    Thread.Sleep(100);
                    KeyValuePair<string, string> KvpCode = ECode.GetECode();
                    if (KvpCode.Key != string.Empty)        //如果返回的内容非空
                    {
                        ProcessingCode(KvpCode);
                    }
                }
            }
    这部分卸载EXE程序中:  //方法内容不固定
            private void ProcessingCode(KeyValuePair<string, string> KvpCode)
            {
                if (KvpCode.Key == textBox11.Text)
                {
                    SetText(textBox13, KvpCode.Value);
                }
                else if (KvpCode.Key == textBox21.Text)
                {
                    SetText(textBox23, KvpCode.Value);
                }
                else if (KvpCode.Key == textBox31.Text)
                {
                    SetText(textBox33, KvpCode.Value);
                }
                else if (KvpCode.Key == textBox41.Text)
                {
                    SetText(textBox43, KvpCode.Value);
                }
            }
    上面说传参数?怎么传? 这样:? private void StartGetECode(object func)
            {
                GetCodeThread = new Thread(new ThreadStart(this.GetECodeFunc));
                GetCodeThread.IsBackground = true;    //设置为后台线程
                GetCodeThread.Name = "GetCodeThread";
                GetCodeThread.Start(func);        //启动线程
            }
            //线程函数
            private void GetECodeFunc(object func)
            {
                while (ECode.GetListenState())
                {
                    Thread.Sleep(100);
                    KeyValuePair<string, string> KvpCode = ECode.GetECode();
                    if (KvpCode.Key != string.Empty)        //如果返回的内容非空
                    {
                        func    ///怎么用?
                        //ProcessingCode(KvpCode);
                    }
                }
            }
      

  6.   

            public delegate void ProcessingCodeDelegate(KeyValuePair<string, string> KvpCode);        private void GetECodeFunc(ProcessingCodeDelegate fun)
            { 
                KeyValuePair<string, string> KvpCode=new KeyValuePair<string,string>();
                fun.Invoke(KvpCode);
            }
            private void StartGetECode(ProcessingCodeDelegate fun)
            {
                KeyValuePair<string, string> KvpCode = new KeyValuePair<string, string>();
                fun.Invoke(KvpCode);
            }        private void ProcessingCode(KeyValuePair<string, string> KvpCode)
            {
                //todo
            }        /// <summary>
            /// 调用GetECodeFunc、StartGetECode的时候把本地的ProcessingCode方法用委托的形式传进去
            /// </summary>
            private void test()
            {
                ProcessingCodeDelegate pcd = ProcessingCode;
                GetECodeFunc(pcd);
            }
    委托的第一大特性就是可以把方法作为参数传递。
      

  7.   


    Action<KeyValuePair<string, string>> action可以把参数定义成Action,就不用显示的定义委托了,方便。
    没返回值的用Action,有返回值的用Func。
      

  8.   

    可以了,不过我这样写有没有什么问题?或者有什么更好的写法?
    DLL: //委托
    private delegate void ProcessingCodeDelegate(KeyValuePair<string, string> KvpCode); //启动线程
    public Thread StartGetECode(Action<KeyValuePair<string, string>> action)
    {
    try
    {
    GetCodeThread = new Thread(new ParameterizedThreadStart(this.GetECodeFunc));
    GetCodeThread.IsBackground = true; //设置为后台线程
    GetCodeThread.Name = "GetCodeThread";
    GetCodeThread.Start(action); //启动线程
    return GetCodeThread;
    }
    catch (System.Exception ex)
    {
    MessageBox.Show(ex.ToString());
    return null;
    }
    } //线程函数
    private void GetECodeFunc(object action)
    {
    Thread.Sleep(2000);
    while (this.GetListenState())
    {
    Thread.Sleep(100);
    KeyValuePair<string, string> KvpCode = this.GetECode();
    if (KvpCode.Key != string.Empty) //如果返回的内容非空
    {
    ProcessingCodeDelegate Call = new ProcessingCodeDelegate((Action<KeyValuePair<string, string>>)action);
    Call.Invoke(KvpCode);
    }
    }
    }
    EXE: //启动
    public void StartECode()
    {
    try
    {
    ECode = new ECode_AC(MySqlServer, MySqlPort, database, username, password);
    ECode.StartListen(ListenIP, Port, UserID);
    ECode.StartGetECode(ProcessingCode);
    }
    catch (System.Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }
    } //处理函数
    private void ProcessingCode(KeyValuePair<string, string> KvpCode)
    {
    if (KvpCode.Key == textBox11.Text)
    {
    SetText(textBox13, KvpCode.Value);
    }
    }
      

  9.   

    ParameterizedThreadStart好像只能传object,传进去再强制转换,会不会有问题?
    用起来好像没问题