[英]How to invoke a form inside another form task
In my Windows Form application I have created 2 Forms.在我的 Windows 窗体应用程序中,我创建了 2 个窗体。 In form 1 when I click
button1
, a new task will start.在表单 1 中,当我单击
button1
,将启动一个新任务。 Inside the task I have created an instance of the form2
and show form2
.在任务中,我创建了
form2
一个实例并显示form2
。 I am calling the showData
Method of Form2
.我正在调用
Form2
的showData
方法。
//Form1
public event TickHandler Tick;
public EventArgs e = null;
public delegate void TickHandler(int a1, EventArgs e);
private void button1_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(() =>
{
Form2 form2 = new Form2();
form2.Show();
}
}
//Form2
public void showData(Form1 m)
{
m.Tick += new Form1.TickHandler(test);
}
public void test(int a1,EventArgs e)
{
Task.Factory.StartNew(() =>
{
for (int i = a1; i < 1000; i++)
{
label1.Invoke(new MethodInvoker(delegate { label1.Text = i.ToString(); }));
}
});
}
As kenny suggested i have modified the code.正如肯尼建议我修改了代码。 now it running how i am expected.
现在它按照我的预期运行。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Task.Factory.StartNew(() =>
{
Action act1 = (() =>
{
Form2 form2 = new Form2();
form2.StartPosition = FormStartPosition.CenterParent;
form2.Show();
});
this.BeginInvoke(act1);
});
}
}
// FORM2 // 表格 2
private void Form2_Load(object sender, EventArgs e)
{
test(1);
}
public void test(int a1)
{
Task.Factory.StartNew(() =>
{
for (int i = a1; i < 1000; i++)
{
label1.Invoke(new MethodInvoker(delegate { label1.Text = i.ToString(); }));
}
});
}
Once again thanks Kenny再次感谢肯尼
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.