您好,我在运行时向表单添加控件时遇到困难。
static Form1 f2 = new Form1();
async public static void startTimer()
{
//await Task.Run(() => f2.ShowDialog());
stopWatch.Start();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 50;
aTimer.Enabled = true;
await Task.Run(() => f2.ShowDialog());
}
public static void splitTime()
{
TextBox txtRun = new TextBox();
txtRun.Name = "txtDynamic";
txtRun.Location = new System.Drawing.Point(20, 18 + (20 * 2));
txtRun.Size = new System.Drawing.Size(200, 25);
f2.Controls.Add(txtRun);
}
知道为什么这会失败吗?我认为是因为异步,但不知道如何解决。
this.BeginInvoke((Action)delegate ()
{
TextBox txtRun = new TextBox();
txtRun.Name = "txtDynamic";
txtRun.Location = new System.Drawing.Point(20, 18 + (20 * 2));
txtRun.Size = new System.Drawing.Size(200, 25);
f2.Controls.Add(txtRun);
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句