C#计时器问题 [英] C# Timer Issue

查看:122
本文介绍了C#计时器问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在为"Simon"游戏遍历堆栈时遇到麻烦.我创建了一个随机字符串生成器来填充要迭代的堆栈以触发计时器,而我只能让它反复遍历相同的索引.我如何获取它返回timer()并转到堆栈中的下一项(mySimon)? (当然,不使用Pop().)现在,它确实无限循环,如您所见.为了节省空间,我省略了每种颜色的情况,仅包括绿色,因为它们都是相同的. 绿色"是指闪烁的实际按钮.实际上,每次也会从播放按钮调用timer().随时更改我的代码,然后将其与任何建议一起重新发布.谢谢!

Having trouble iterating through a stack for a "Simon" game. I created a random string generator to populate a stack to iterate through to trigger timers, and I can only get it to iterate the same index over and over. How do I get it to return to timer() and go to the next item in the stack (mySimon)? (without using Pop(), of course.) Cause right now, it just loops infinitely as you can surely see. To save space, I omitted the cases for each color and only included green because they are all identical. "green" refers to the actual button that flashes. timer() is actually called from a playback button each time, as well. Feel free to alter my code and re-post it with any suggestions. Thanks!

我应该补充一点,即1毫秒的间隔很好,因为那只是递增的刻度.我要发生的实际眨眼就足够了,我的麻烦是使它正确循环.

I should add that the interval of 1 millisecond is fine, because that''s just the tick that''s getting incremented. The actual blink I''m getting to happen is sufficient, my trouble is with getting it to loop properly.

 

public Window1()
{
    InitializeComponent();
}
public void timer()
{
    foreach (string i in mySimon)
        if (isRunning == false)
        {
            switch (i)
            {
                case "myGreen":
                {
                    DispatcherTimer timer = new DispatcherTimer();
                    timer.Interval = TimeSpan.FromMilliseconds(1);
                    timer.Tick += new EventHandler(greenCount);
                    timer.Start();
                    isRunning = true;
                    Thread.Sleep(100);
                    break;
                }
            }
        }
}
public void greenCount(Object sender, EventArgs args)
{
    counter++;
    simonText.Text = counter.ToString();
    if (counter < 29)
    {
        green.Background = Brushes.LawnGreen;
    }
    if (counter > 29)
    {
        DispatcherTimer myTimer = (DispatcherTimer)sender;
        myTimer.Stop();
        counter = 0;
        green.Background = Brushes.Green;
        isRunning = false;
        timer();
    }
}

推荐答案

timer.Interval = TimeSpan.FromMilliseconds(1);
timer.Interval = TimeSpan.FromMilliseconds(1);

 

这太疯狂了.您认为您可以看到一毫秒? 30毫秒太短了.如果要在其中使用一个计数器,为什么还要有一个计时器呢?为什么不将计时器设置为500毫秒,然后每次都交换一次?

This is insane.  You think you can see one millisecond ? 30 milliseconds is WAY too short.  And why have a timer if you''re going to use a counter inside it ? Why not set the timer to 500 milliseconds and just swap every time ?


您是在第一次设置后将 isRunning 设置为true迭代.由于您的for循环中的if语句会检查 isRunning ,因此它将跳过for循环中除第一项以外的所有内容.

当您说 timer.Start()时,直到 DispatcherTimer 的工作方式……它们仅在UI线程上运行).

不确定要在这里完成什么,但是肯定不是这样做的方法.
You are setting isRunning to true after the first iteration. Since the if statement inside your for loop checks isRunning, it will skip over all but the first item in the for loop.

When you say timer.Start(), the greenCount() method will never actually be reached until the timer() method has completed (that''s how DispatcherTimers work... they only run on the UI thread).

Not sure what you are trying to accomplish here, but surely this is not the way to do it.


这篇关于C#计时器问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆