110,546
社区成员
发帖
与我相关
我的任务
分享
myForm mForm= null;//myForm是之前定义的一个窗体类
private delegate void OnClose();//定义委托
//省略其他代码。。。
//主线程按钮点击事件
private void button1_Click(object sender, EventArgs e)
{
//创建弹出窗体
mForm= new myForm (true, 1, 100, ProgressBarStyle.Continuous);
mForm.Show();
//新线程
Thread mThread = new Thread(new ThreadStart(DoFor));
mThread.Start();
}
private void DoFor()
{
for (int i = 1; i <= 100; i++)
{
System.Threading.Thread.Sleep(10);
mForm.OnSetValue(i);//这里改变弹出窗口的一个TextBox的Text属性
}
//循环结束后
this.Invoke(new OnClose(DoClose));//子线程中关闭主线程创建的对象
}
//委托指向的方法
private void DoClose()
{
mForm.Close();
}
}
public delegate void SetIntValue(int value);
public void setX(int value)
{
if (InvokeRequired)
{
// 在子线程中调用此方法时,通过Invoke转成主线程执行
Invoke(new SetIntValue(setX), value);
return;
}
// 设置X值并显示
X = value;
textbox.Text = X.toString();
}
public delegate void SetIntValue(int value);
public void setX(int value)
{
if (InvokeRequired)
{
// 在子线程中调用此方法时,通过Invoke转成主线程执行
Invoke(new SetIntValue(value));
return;
}
// 设置X值并显示
X = value;
textbox.Text = X.toString();
}
private void button1_Click(object sender, EventArgs e)
{
Thread th = new Thread(aa);
th.Start();
}
delegate void somedle();
private void aa()
{
if (this.InvokeRequired)
{
somedle sd = new somedle(aa);
this.Invoke(sd);
return;
}
ShowChar('A');
}
public void ShowChar(char ch)
{
lock (this)
{
textBox1.Text += ch;
}
}