是你代码的问题,因为MessageBox不在主线程private void YYY(String text)
{
if (this.InvokeRequired)
            {
Control.Invoke(new XXX(YYY), new Object[] { "hello"});
            }
            else
            {   MessageBox.Show(text);
   Label1.Text = text;
}
}
  
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 最新版本:20070130http://www.cnblogs.com/feiyun0112/archive/2006/09/20/509783.html

解决方案 »

  1.   

    是你代码的问题,因为MessageBox不在主线程private void YYY(String text)
    {
    if (this.InvokeRequired)
    {
    Control.Invoke(new XXX(YYY), new Object[] { "hello"});
    }
    else
    {MessageBox.Show(text);
    Label1.Text = text;
    }
    }>>>我在子线程中已经使用了"Control.Invoke(new XXX(YYY), new Object[] { "hello"});"为何在YYY()中还要使用"Control.Invoke(new XXX(YYY), new Object[] { "hello"});"呢?
      

  2.   

    而且该成了和feiyun0112所说的一样,还是会出现主帖里面的问题...
      

  3.   

    用同一对象判断if (Label1.InvokeRequired)
    {
    Label1.Invoke(new XXX(YYY), new Object[] { "hello"});
      

  4.   

    将参数封装->切换到主线程->执行函数->切换回调用线程(工作线程)
    --------------------------------------------------------------
    你可能理解错了
    invoke只不过使用安全线程来调用而已,并不是主线程
      

  5.   

    楼上的弟兄,MSDN是这么说的:Control.Invoke 在拥有此控件的基础窗口句柄的线程上执行委托。那到底是不是在主线程中执行的啊?
      

  6.   

    ListView,ListBox,TextBox等继承自Control的控件...
      

  7.   

    我觉得invoke就是.net又new了一个新的线程来执行委托的任务,只不过这个新线程的控制权是属于调用这个委托的线程。
      

  8.   

    但是这些控件可能是主线程创建,也可能子线程>>>但我的测试代码里的控件全部都是主线程创建的...
      

  9.   

    你知道,它又不知道>>>-_-! "它"是指?
      

  10.   

    >> Control.Invoke(new XXX(YYY), new Object[] { "hello"});等同于XXX x = new XXX(YYY);
    Object[] y = new Object[] { "hello" };
    Control.Invoke(x, y);这样调用new XXX(YYY)是在进入Control.Invoke之前就执行了,当然还是在Main Thread。
    线程切换是在Invoke函数里面完成的。
      

  11.   

    sorry, 没意识到XXX是你得delegate... 把你调用Invoke的完整代码贴上来,例如,你用的具体是哪一个Control...
    我试了一下,从工作线程Invoke过来的行为和主线程直接调用是一样的。    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        public delegate void XXX(string msg);        private void YYY(String text)
            {
                MessageBox.Show(text);
            }        private void button1_Click(object sender, EventArgs e)
            {
                YYY("hello from Main Form");
            }        public void GotoWorkThread(object state)
            {
                Invoke(new XXX(YYY), new object[] { "hello from Work Thread" });
            }        private void button2_Click(object sender, EventArgs e)
            {
                ThreadPool.QueueUserWorkItem(
                    new WaitCallback(GotoWorkThread)
                );
            }
        }
      

  12.   

    To qqchen79(知秋一叶):private delegate void DGUpdateCntrList(String value);private void UpdateCntrList(String value)
    {
       //lbCntrList 是一个ListBox控件
       lbCntrList.BeginUpdate();
       lbCntrList.Items.Add(value);
       lbCntrList.SetSelected(lbCntrList.Items.Count -1, true);
       lbCntrList.EndUpdate();   MessageBox.Show("Just a test");
    }子线程中调用this.Invoke():this.Invoke(new DGUpdateCntrList(UpdateCntrList), new Object[] { content });弹出的对话框就像直接在线程中执行MessageBox.Show()一样的,不知道何解,望指教!谢谢!!
      

  13.   

    嗯....发现了一个有趣的现象,如果invoke是在自定义的线程或者线程池中被执行的话,那么对话框的确和在主线程中直接执行是一样的,但是如果在Socket的异步回调函数中执行的话,就会出现主帖所说的问题了...到底是怎么一回事呢?
      

  14.   

    1. Can you confirm the call back actually happened on the main thread by checking the thread id (Thread.CurrentThread.ThreadID).2. This could also be related to how MessageBox.Show finds the active window. Can you try MessageBox.Show(this, text) instead?
      

  15.   

    to qqchen79(知秋一叶):1. 判断this.InvokeRequest的确为true,也就是说是从其他线程切换进来的.2. 用MessageBox.Show(this, text)可以显示一个模式对话框,但这个方法如果是在工作线程中直接使用,也会在主窗体中显示一个模式对话框.
    呵呵.....郁闷,搞不明白什么回事.
      

  16.   

    change
    private void YYY(String text)
    {
    MessageBox.Show(text);
    Label1.Text = text;
    }
    with
    private void YYY(String text)
    {
    MessageBox.Show(this, text);
    Label1.Text = text;
    }
      

  17.   

    给你写了一个例子程序,按下按钮,每隔1秒钟,向窗体上面的listView1添加一条记录,应该可以解决你的问题
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace Test
    {
        public delegate void CustomEventHandler(string message);    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                WorkThread worker = new WorkThread();
                worker.CustomEvent += new CustomEventHandler(worker_CustomEvent);
                worker.Start();
            }        void worker_CustomEvent(string message)
            {
                this.Invoke(new CustomEventHandler(this.AddItemToListView), message);
            }
            private void AddItemToListView(string message)
            {
                listView1.Items.Add(new ListViewItem(message));
            }
        }    public class WorkThread
        {        int i = 0;
            public event CustomEventHandler CustomEvent = null;        public void Start()
            {
                System.Threading.Thread thread = new System.Threading.Thread(
                    new System.Threading.ThreadStart(this.Do));
                thread.IsBackground = true;
                thread.Start();
            }        private void Do()
            {
                while (true)
                {
                    if (CustomEvent != null)
                    {
                        i++;
                        CustomEvent("Count : " + i);
                    }
                    System.Threading.Thread.Sleep(1000);
                }
            }
        }
    }
      

  18.   

    要得到模式对话框用  Control.BeginInvoke()
      

  19.   

    平时用都没问题的,至于为什么你说Socket回调函数里用就会出错,这就难以解释了。
      

  20.   

    平时用都没问题的,至于为什么你说Socket回调函数里用就会出错,这就难以解释了。>>>>>的确...同一个YYY(String text),在普通线程中执行Control.Invoke()就可以弹出模式对话框,但是在Socket的BeginXXX回调函数中却不可以,实在是想不明白.----_要得到模式对话框用 Control.BeginInvoke()>>>>>这个...没试过