是否是用带参数的委托?我不明白,请高手给点简洁代码

解决方案 »

  1.   

    完整源代码: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 Updating_UI_Controls
    {
        public partial class Form1 : Form
        {
            private bool processingDone;
            private Thread myWorkerThread;        public Form1()
            {
                InitializeComponent();
            }        private void button2_Click(object sender, EventArgs e)
            {
                processingDone = true;
                myWorkerThread.Join();
                button2.Enabled = false;
                button1.Enabled = true;
            }        private void ExecuteProcessing()
            {
                UpdateLabelDelegate labelUpdater = new UpdateLabelDelegate(UpdateLabel);
                this.Invoke(labelUpdater, new object[]{"Processing: Active", Color.Red});            try
                {
                    while (!processingDone)
                    {
                        Thread.Sleep(0);
                        this.BeginInvoke(labelUpdater, new object[] { "Processing: Still Active - " + DateTime.Now, Color.Green });
                    }
                }
                finally
                {
                    Thread.Sleep(1000);
                    this.BeginInvoke(labelUpdater, new object[] { "Processing: Done", Color.Black });
                }
            }        delegate void UpdateLabelDelegate(string labelText, Color textColor);
            private void UpdateLabel(string labelText, Color textColor)
            {
                label1.Text = labelText;
                label1.ForeColor = textColor;
            }        private void button1_Click(object sender, EventArgs e)
            {
                button1.Enabled = false;
                button2.Enabled = true;
              
                processingDone = false;            myWorkerThread = new Thread(new ThreadStart(ExecuteProcessing));
                myWorkerThread.Start();
            }
        }
    }
      

  2.   

    一个最简单的办法
      在formload事件里面加入Control.CheckForIllegalCrossThreadCalls = false;就可以了。一般这个问题都出现在.net2005里面,我也是菜鸟只知道这么个方法~。。看看还有没有大大可以给出更好的
      

  3.   

    给你点简单的代码:
    定义一个函数,用来更新控件,可以带参数的
    void Set()
            {
                TextBox1.Text="这是线程更新的控件值";
            }
    然后定义相应的委托
    delegate void A();
    定义线程调用的函数
    void MyThread()
            {
    A B= new A(Set);//指向更新函数
    this.Invoke(B, null);//调用函数
    }
    运行线程
                Thread MyThread1;
                MyThread1 = new Thread(new ThreadStart(MyThread));
                MyThread1.Start();
      

  4.   


    private delegate void MyMethod();void OpenDialogOnOtherThread()
    {MyMethod OpenDialog = new MyMethod(this.MyOpenDialog);
    this.Invoke(OpenDialog);
    }void MyOpenDialog()
    {
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.ShowDialog();
    }private void button1_Click(object sender, EventArgs e)
    {
    Thread openDialog=new Thread(new ThreadStart(this.OpenDialogOnOtherThread));
    openDialog.Start();
    }
      

  5.   

    如果要在一个线程里面设置窗口中label的文字,调用函数SetText。delegate void SetTextCallBack(string text);void SetText(string text)
    {
        if (this.InvokeRequired)
        {
            SetEnabledCallBack cb = new SetEnabledCallBack(SetText);
            this.Invoke(cb, new object[] { text });
        }
        else
        {
         this.label.Text = text;
        }
    }
      

  6.   

    sorry,上面的代码中的SetEnabledCallBack应该改成SetTextCallBack。从自己的代码里粘的:)delegate void SetTextCallBack(string text);void SetText(string text)
    {
        if (this.InvokeRequired)
        {
            SetTextCallBack cb = new SetTextCallBack(SetText);
            this.Invoke(cb, new object[] { text });
        }
        else
        {
         this.label.Text = text;
        }
    }
      

  7.   

    谢谢各位回复的高手,不过现在有个问题,我运行的线程方法是另外一个专门类中的,在这个类里面this.Invoke不让用啊!有什么办法解决?
      

  8.   

    在你的主程序里面(即主线程所在的类里面),写个委托的函数把你的专门的类包起来。UpdateLabelDelegate labelUpdater = new UpdateLabelDelegate(UpdateLabel);
    this.BeginInvoke(labelUpdater, new object[]{"Processing: Active", Color.Red});...private void UpdateLabel(string labelText, Color textColor)
    {
      Class aaa = new aaa();//事例化你的专门类
      aaa.UpdateUI();//调用你的更新ui的方法。
      //label1.Text = labelText;
      //label1.ForeColor = textColor;
     }
      

  9.   

    我看不明白楼上的,是不是我说的不清楚?我现在面对的问题是这样的,在我的主程序里面,我要运行一个线程,这个线程的入口方发为另外一个类,假设为CLS,现在我要在这里CLS类里面修改主界面的控件内容,控件内容可能为A也可能为B,所以必须用参数传递,我想在CLS类里面委托调用主界面的方法,可是用this.Invoke出错,非静态的字段、方法或属性“System.Windows.Forms.Control.Invoke(System.Delegate, object[])”要求对象引用
    我不知道应该怎么解决?我的思路是否正确?
      

  10.   

    delegate void SetTextCallBack(string text);public void SetText(string text)
    {
        if (this.InvokeRequired)
        {
            SetTextCallBack cb = new SetTextCallBack(SetText);
            this.Invoke(cb, new object[] { text });
        }
        else
        {
         this.label.Text = text;
        }
    }把上面的代码写在主线程里。在辅助线程里直接调用SetText不行吗?