是你代码的问题,因为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
{
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
{
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"});"呢?
{
Label1.Invoke(new XXX(YYY), new Object[] { "hello"});
--------------------------------------------------------------
你可能理解错了
invoke只不过使用安全线程来调用而已,并不是主线程
Object[] y = new Object[] { "hello" };
Control.Invoke(x, y);这样调用new XXX(YYY)是在进入Control.Invoke之前就执行了,当然还是在Main Thread。
线程切换是在Invoke函数里面完成的。
我试了一下,从工作线程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)
);
}
}
{
//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()一样的,不知道何解,望指教!谢谢!!
呵呵.....郁闷,搞不明白什么回事.
private void YYY(String text)
{
MessageBox.Show(text);
Label1.Text = text;
}
with
private void YYY(String text)
{
MessageBox.Show(this, text);
Label1.Text = text;
}
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);
}
}
}
}