我正在尝试编写一个Windows窗体应用程序,该应用程序将一直停止到单击按钮为止(没有无限循环来检查标志或类似的东西) 现在我正在使用第二个线程来执行程序,直到事件发生为止
当前代码:
Id
Start()是我所有代码所在的位置,我通过以下方式停止执行:
static void Main(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public Form1()
{
InitializeComponent();
new Thread(Start).Start();
}
问题是每次我更改需要使用委托的形式的东西时,例如:
static AutoResetEvent handle = new AutoResetEvent(false);
public void Start()
{
Massage("Hello World", "Continue");
//stop now
handle.WaitOne();
//after button_click
Massage("user pushed the button", "Continue");
}
private void NextButton_Click(object sender, EventArgs e)
{
handle.Set();
}
我试图在没有第二个线程的情况下做同样的事情,但是比执行停止时,所有表单都冻结(而且我无法按下按钮)。
问题是,是否有任何方法可以停止程序,直到button_click为止,并且仍然允许您不使用
来访问表单对象 public void Massage(string massage, string button){
if (this.Action.InvokeRequired)
{
StringArgReturningVoidDelegate d = new StringArgReturningVoidDelegate(Massage);
this.Invoke(d, new object[] { massage, button });
}
else
{
this.Action.Text = massage;
this.NextButton.Text = button;
}
}
任何时候您想更改表单中的内容吗?
抱歉英语不好,非常感谢您的帮助。