简体   繁体   English

需要循环暂停以允许用户按c#中的按钮

[英]Need While Loop to pause to allow user to press a button in c#

I need help stopping this loop from constantly looping, i need it to give the user time to choose something in the new form it opens up but currently it just opens a whole load of them and doesn't stop. 我需要帮助阻止这个循环不断循环,我需要它给用户时间选择它打开的新形式的东西,但目前它只是打开它们的整个负载,并没有停止。

while (Convert.ToInt32(PlayerHP[PlayerNo]) > 0 && Convert.ToInt32(PlayerHP[PlayerNo+1]) > 0){
    ChooseAttack();
    PlayerHP[PlayerNo+1] = (Convert.ToInt32(PlayerHP[PlayerNo+1]) - Damage()).ToString();
    lblHPP1.Text = PlayerHP[0];
    lblHPP2.Text = PlayerHP[1];
    PlayerNo = ((PlayerNo+1)%2);
    Application.DoEvents();
}

I've been experimenting with the "Application.DoEvents()" line but whereever i place it it doesnt do anything. 我一直在试验“Application.DoEvents()”行,但无论我把它放在哪里都没有做任何事情。

I don't see a form anywhere, but expect that somewhere in that ChooseAttack(); 我没有在任何地方看到任何形式,但期望在ChooseAttack();中的某个地方ChooseAttack(); call you have some code that looks like this: 打电话给你有一些看起来像这样的代码:

frmChoose.Show();

And all you need to do is change it to this: 您需要做的就是将其更改为:

frmChoose.ShowDialog();

I would push the loop out onto a timer thread so you can control the "loop" externally. 我会将循环推送到计时器线程,以便您可以在外部控制“循环”。 The speed at which the loop runs would be the timeout time of the timer. 循环运行的速度将是计时器的超时时间。

For more clarity, you would no longer have a 'while' statement, more of an if at the top of the timer's call back method to check your conditions. 为了更清楚,你将不再有'while'语句,更多的是在计时器的回调方法的顶部检查你的条件。 The actual looping part of it would be taken care of by the timer executing over and over. 它的实际循环部分将由反复执行的定时器来处理。

Depending on which timer you choose, when you click a button on the form, you would just say Timer.Enabled = false; 根据您选择的计时器,当您单击表单上的按钮时,您只需说Timer.Enabled = false; or something similar. 或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM