我正在创建一个控制台应用程序,该应用程序创建一个表单以提醒某些给定状态的用户 - 稍后,代码库将成为一个类库。然而,现在,我需要显示窗体(ShowDialog将是这里最好的方法,我猜)然后在窗体关闭之前调用任意方法。作为一个例子,我需要显示表单,设置标签控件的文本值,等待n秒,然后更改标签的值,然后关闭表单。我知道这听起来有些微不足道,但我试图对设计进行概念验证。C#WinForms ShowDialog()调用任意方法
我看了一下,看起来好像这是不可能的,因为ShowDialog()要求我在调用方法/类中继续执行代码清单之前关闭窗体。
这是我到目前为止有:
PopUpForm myForm = new PopUpForm(string messageToDisplay);
myForm.ShowDialog();
//call myForm.someMethod() here, before the form closes
//dispose of the form, now that we've no use for it
myform.Dispose();
//target method in PopUpform class
public void someMethod()
{
lblText.Text = "Waiting for some reason";
//wait n number of seconds
lblText.Text = "Finished waiting. Form will now close";
//it doesn't matter if the form closes before the user can see this.
}
它看起来像的ShowDialog()不支持这种行为。我正在研究BackgroundWorker线程,但想知道是否有人对此有任何建议,或者之前遇到过这个问题。
的初步评论。“但是,如果这纯粹是一个控制台应用程序(并且没有消息泵),那么这可能无法正常工作。”目前,我同意你的看法,我使用表单作为一种方式,以直观的方式显示最终版本中提出的事件(将代替表单)的样子。取而代之,我可能只是开展一些活动,并在其他地方抓住他们。 – 2013-03-07 08:46:04