我要做的是启动一个函数,然后将bool改为false,等一下再将其变为true。但是,我想在不需要等待功能的情况下完成它,我该如何做呢?
我只能使用视觉C# 2010快车。
这是有问题的代码。我尝试接收用户输入(例如,右箭头)并相应移动,但在字符移动时不允许进一步输入。
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;
}发布于 2015-06-14 06:20:59
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();
}
}发布于 2015-06-14 07:57:52
我意识到答案已经被接受了,我很喜欢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.这样,布尔标志将始终在UI线程上读取/写入,并且不需要任何类型的额外锁定来确保正确性。
在本例中,代码如下所示:
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.
}
}https://stackoverflow.com/questions/30826490
复制相似问题