首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不睡觉就等着?

不睡觉就等着?
EN

Stack Overflow用户
提问于 2015-06-14 06:15:43
回答 2查看 4.3K关注 0票数 4

我要做的是启动一个函数,然后将bool改为false,等一下再将其变为true。但是,我想在不需要等待功能的情况下完成它,我该如何做呢?

我只能使用视觉C# 2010快车。

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

代码语言:javascript
复制
        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;  
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-14 06:20:59

使用Task.Delay

代码语言:javascript
复制
Task.Delay(1000).ContinueWith((t) => Console.WriteLine("I'm done"));

代码语言:javascript
复制
await Task.Delay(1000);
Console.WriteLine("I'm done");

对于旧的框架,您可以使用以下方法:

代码语言:javascript
复制
var timer = new System.Timers.Timer(1000);
timer.Elapsed += delegate { Console.WriteLine("I'm done"); };
timer.AutoReset = false;
timer.Start();

根据问题中的描述,示例:

代码语言:javascript
复制
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();
    }
}
票数 11
EN

Stack Overflow用户

发布于 2015-06-14 07:57:52

我意识到答案已经被接受了,我很喜欢ixSci的回答,他建议使用Timer对象来实现OP的目标。

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

它必须看起来像这样:

代码语言:javascript
复制
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线程上读取/写入,并且不需要任何类型的额外锁定来确保正确性。

在本例中,代码如下所示:

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30826490

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档