没有睡觉等待?

时间:2015-06-14 06:15:43

标签: c# function wait

我要做的是启动一个函数,然后将bool更改为false,等待一秒钟再将其变为true。但是我想在没有等待功能的情况下这样做,我该怎么做?

我只能使用Visual C#2010 Express。

这是有问题的代码。我正在尝试接收用户输入(例如右箭头)并相应地移动,但在角色移动时不允许进一步输入。

        x = Test.Location.X;
        y = Test.Location.Y;
        if (direction == "right") 
        {
            for (int i = 0; i < 32; i++)
            {
                x++;
                Test.Location = new Point(x, y);
                Thread.Sleep(31);
            }
        }
    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        int xmax = Screen.PrimaryScreen.Bounds.Width - 32;
        int ymax = Screen.PrimaryScreen.Bounds.Height - 32;
        if (e.KeyCode == Keys.Right && x < xmax) direction = "right";
        else if (e.KeyCode == Keys.Left && x > 0) direction = "left";
        else if (e.KeyCode == Keys.Up && y > 0) direction = "up";
        else if (e.KeyCode == Keys.Down && y < ymax) direction = "down";

        if (moveAllowed)
        {
            moveAllowed = false;
            Movement();
        }
        moveAllowed = true;  
    }

2 个答案:

答案 0 :(得分:10)

使用Task.Delay

Task.Delay(1000).ContinueWith((t) => Console.WriteLine("I'm done"));

await Task.Delay(1000);
Console.WriteLine("I'm done");

对于较旧的框架,您可以使用以下内容:

var timer = new System.Timers.Timer(1000);
timer.Elapsed += delegate { Console.WriteLine("I'm done"); };
timer.AutoReset = false;
timer.Start();

根据问题中的描述示例:

class SimpleClass
{
    public bool Flag { get; set; }

    public void function()
    {
        Flag = false;
        var timer = new System.Timers.Timer(1000);
        timer.Elapsed += (src, args) => { Flag = true; Console.WriteLine("I'm done"); };
        timer.AutoReset = false;
        timer.Start();
    }
}

答案 1 :(得分:0)

我意识到答案已被接受,我确实喜欢java.lang.IllegalStateException: Fragment already added: MainMenuFragment{42c64d90 #0 id=0x7f0b003f MainMenuFragment} 的答案,他建议使用ixSci对象来实现OP的目标。< / p>

但是,使用Timer特别引入了线程注意事项。并且为了确保在这种情况下的正确性,需要更多代码来正确地同步布尔标志值。基本上,在读取或写入标志的任何地方,代码区域都需要在其周围定义一个锁定语句。

它必须看起来像这样:

System.Timers.Timer

或者,为了避免考虑线程,也许OP可以考虑使用private final object flagLock = new object(); private bool moveAllowed = true; private System.Timers.Timer timer = new System.Timers.Timer(); public Form1() { this.timer.Interval = 1000; this.timer.AutoReset = false; this.timer.Elapsed += (s, e) => { // this DOES NOT run on the UI thread, so locking IS necessary to ensure correct behavior. this.timer.Stop(); lock (this.flagLock) { this.moveAllowed = true; } }; } // The code in this event handler runs on the UI thread. private void Form1_KeyDown(object sender, KeyEventArgs e) { // Locking is necessary here too. lock (this.flagLock) { if (this.moveAllowed) { this.moveAllowed = false; Movement(); this.timer.Start(); // wait 1 second to reset this.moveAllowed to true. } } } 类的不同风格。即:Timer。这样,布尔标志将始终在UI线程上读/写,并且不需要任何额外的锁定来确保正确性。

在这种情况下,代码看起来像这样:

System.Windows.Forms.Timer
相关问题