public Thread t1;
delegate void DoThingCallBack();
private void button2_Click(object sender, EventArgs e)
{
string name = "100";
t1 = new Thread(new ThreadStart(c1));
t1.Start();
}
private void c1()
{
if (webBrowser1.InvokeRequired)
{
DoThingCallBack cb = new DoThingCallBack(c1);
this.Invoke(cb);
}
else
{
label1.text = "???";
}
}这段代码中,如何把string name的值传送到label1.text中?
delegate void DoThingCallBack();
private void button2_Click(object sender, EventArgs e)
{
string name = "100";
t1 = new Thread(new ThreadStart(c1));
t1.Start();
}
private void c1()
{
if (webBrowser1.InvokeRequired)
{
DoThingCallBack cb = new DoThingCallBack(c1);
this.Invoke(cb);
}
else
{
label1.text = "???";
}
}这段代码中,如何把string name的值传送到label1.text中?
delegate void DoThingCallBack(string xxx);
//给你个例子
public partial class Form1 : Form
{
public delegate void MyEventHandler(string username);//与所调用的方法中的参数相同
public event MyEventHandler myevent;
public Form1()
{
InitializeComponent(); MyClass c = new MyClass();
this.myevent +=new MyEventHandler(c.SayHello);
myevent("xiao");
}
}
public class MyClass
{
public void SayHello(string username)
{
MessageBox.Show("Hello," + username);
}
}
private delegate void ShowTextInLable(string str); private void button1_Click(object sender, EventArgs e)
{
Thread t = new Thread(showMethod);
t.Start();
} private void showMethod()
{
ShowTextInLable s = new ShowTextInLable(ShowInLable);
this.Invoke(s, new object[] { "test" });
} private void ShowInLable(string str)
{
lable1.AppendText(str);
}
之前给人个例子是rtf的,忘改回来了。只改了前面的变量名。
{
Thread th = new Thread(delegate()
{
int i = 100;
while (i-- > 0)
{
this.Invoke(new EventHandler((s, e1) => textBox2.AppendText("a")));
}
});
th.Start();
}