c# - Waiting without Sleeping? -
what i'm trying start function, change bool false, wait second , turn true again. i'd without function having wait, how do this?
i can use visual c# 2010 express.
this problematic code. trying receive user input (right arrow example) , move accordingly, not allow further input while character moving.
x = test.location.x; y = test.location.y; if (direction == "right") { (int = 0; < 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; }
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 can use following:
var timer = new system.timers.timer(1000); timer.elapsed += delegate { console.writeline("i'm done"); }; timer.autoreset = false; timer.start();
example according description in 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(); } }
Comments
Post a Comment