Waiting without sleep?

What I'm trying to do is run the function and then change the bool to false, wait a second and turn it back on to true. However, I would like to do this without a function that has to wait, how do I do this?

I can only use Visual C # 2010 Express.

This is the problem code. I am trying to get user input (for example, the right arrow) and move accordingly, but not allow further input while moving the character.

        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;  
    }
+4
source share
2 answers

Use Task.Delay :

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

or

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

For older frameworks you can use the following:

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

An example as described in the question:

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();
    }
}
+9
source

, , ixSci , Timer OP.

, System.Timers.Timer, . . , , , .

:

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.
        }
    }
}

, , , OP Timer. : System.Windows.Forms.Timer. , / , .

:

private bool moveAllowed = true;
private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

public Form1()
{
    this.timer.Interval = 1000;
    this.timer.Tick += (s, e) =>
    {
        // this runs on the UI thread, so no locking necessary.
        this.timer.Stop(); // this call is necessary, because unlike System.Timers.Timer, there is no AutoReset property to do it automatically.
        this.moveAllowed = true;
    };
}

// The code in this event handler runs on the UI thread.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (this.moveAllowed)
    {
        this.moveAllowed = false;
        Movement();
        this.timer.Start(); // wait 1 second to reset this.moveAllowed to true.
    }
}
0

All Articles