为什么Sleep(); function skip Label - >文字命令?

时间:2013-06-08 06:29:41

标签: winforms command-line-interface

在我的编程中,我制作了两个攻击日志(它们是标签),显示战斗期间发生的事情......但每当我运行代码时,两个标签都会跳过所有以前的代码并显示它被告知的最后一件事显示......

this -> EnemyAttackLog -> Text = "[Enemy Attack Log]   " + EName + " Attacked!!";
             Sleep(500);
             damage = rnd4 -> Next(-2,3) + EnemyA;
             if (D > EnemyA)
             {
                 damage = rnd4 -> Next(3,6);
                 this -> AttackLog -> Text = "[Attack Log]    Your Armor Reduced the Damage!";
             }
             Sleep(500);
             this -> EnemyAttackLog -> Text = "[Enemy Attack Log]   " + EName + " did " + damage + " Damage";
             Hp -= damage;
             this -> HealthBar -> Value = Hp;
             Sleep(800);
             damage = rnd4 -> Next(-2,3) + A;
             if (EnemyD > A)
             {
                 damage = rnd4 -> Next(3,6);
                 this -> EnemyAttackLog -> Text = "[Enemy Attack Log]    " + EName + "'s armor reduced damage!";
             }
             Sleep(1500);
             this -> AttackLog -> Text = "[Attack Log]   " + Name + " did " + damage + " Damage";
             EnemyHp -= damage;
             this -> EnemyHealth -> Value = EnemyHp;

如果这样做我认为它会做,它会显示

this -> EnemyAttackLog -> Text = "[Enemy Attack Log]   " + EName + " Attacked!!";

半秒钟然后继续显示下一个半秒钟的代码,但相反,它只是等待所有睡眠功能(加在一起使得3300毫秒)然后显示文本结束......这是

this -> AttackLog -> Text = "[Attack Log]   " + Name + " did " + damage + " Damage";

为什么它会跳过所有以前的命令(对于标签文本)但是STILL会等待3300毫秒?

1 个答案:

答案 0 :(得分:0)

这可能是因为你正在睡觉UI线程。我刚刚在C#中尝试过类似的测试,我也遇到了同样的问题。要解决此问题,您必须在设置值后调用文本框上的“刷新”。

另一种方法是在后台线程上完成工作,然后将“this-> EnemyAttackLog-> Text = sometext”代码封送到UI线程上。这样您的用户界面就会保持响应。