【问题标题】:Wait until Timer has elapsed until next instructions is executed等到 Timer 结束,直到执行下一条指令
【发布时间】:2015-11-19 05:43:43
【问题描述】:

背景:首先我知道我的一些代码很乱,请不要讨厌我。基本上,我想为 Windows 窗体窗口上的 TextBox 对象的移动设置动画。我设法通过使用Timer 做到了这一点。但是,我的其余代码在计时器运行时执行(因此 TextBox 仍在移动)。我该如何阻止这种情况发生。

这是我的一些代码:

move方法:

        private void move(int x, int y)
    {
        xValue = x;
        yValue = y;

        // Check to see if x co-ord needs moving up or down
        if (xValue > txtData.Location.X) // UP
        {
            xDir = 1;
        }
        else if (xValue < box.Location.X) // DOWN
        {
            xDir = -1;
        }
        else // No change
        {
            .xDir = 0;
        }

        if (yValue > box.Location.Y) // RIGHT
        {
            yDir = 1;
        }
        else if (yValue < Location.Y) // LEFT
        {
            yDir = -1;
        }
        else // No change
        {
            yDir = 0;
        }

        timer.Start();
    }

定时器Tick方法:

private void timer_Tick(object sender, EventArgs e)
{
        while (xValue != box.Location.X && yValue != box.Location.Y)
        {
            if (yDir == 0)
            {
                box.SetBounds(box.Location.X + xDir, box.Location.Y, box.Width, box.Height);
            }
            else
            {
                box.SetBounds(box.Location.X, box.Location.Y + yDir, box.Width, box.Height);
            }
        }
}

move 来电:

        move(478, 267);
        move(647, 267);
        move(647, 257);

【问题讨论】:

  • 你不想在进入while循环之前禁用计时器,当所有任务都完成后,然后将计时器设置为启用..
  • “我的其余代码在计时器运行时执行”——剩下的代码是什么?看来您正在使用 System.Windows.Forms.Timer(.NET 中有许多 Timer 类),如果是这样,则在 while 循环在 Tick 事件处理程序中执行时不会发生任何其他事情(甚至不更新窗口的视觉外观)。目前尚不清楚哪些其他代码可能正在运行,这将是一个问题。鉴于此处发布的代码,您似乎遇到了相反的问题(即没有其他代码会运行)
  • 请提供可靠地重现您的问题的a good, minimal, complete code example。准确而具体地说明该代码的功能以及它与您希望它做什么的不同之处。
  • 我希望它使用 478、267 参数和 THEN 647、267 参数运行 while 循环。

标签: c# winforms timer


【解决方案1】:

我不确定您到底在问什么,但如果您试图强制程序停止运行代码直到动画完成,您可以尝试使用异步等待。但是,您至少需要 .Net 4.5 才能使用异步等待。

private async void moveData(int x, int y)
{
    Variables.xValue = x;
    Variables.yValue = y;

    // Check to see if x co-ord needs moving up or down
    if (Variables.xValue > txtData.Location.X) // UP
    {
        Variables.xDir = 1;
    }
    else if (Variables.xValue < txtData.Location.X) // DOWN
    {
        Variables.xDir = -1;
    }
    else // No change
    {
        Variables.xDir = 0;
    }

    // Check to see if y co-ord needs moving left or right
    if (Variables.yValue > txtData.Location.Y) // RIGHT
    {
        Variables.yDir = 1;
    }
    else if (Variables.yValue < txtData.Location.Y) // LEFT
    {
        Variables.yDir = -1;
    }
    else // No change
    {
        Variables.yDir = 0;
    }

    await Animate();
}

private async Task Animate()
{
    while (Variables.xValue != txtData.Location.X && Variables.yValue !=     txtData.Location.Y)
    {
        if (Variables.yDir == 0) // If we are moving in the x direction
        {
            txtData.SetBounds(txtData.Location.X + Variables.xDir, txtData.Location.Y, txtData.Width, txtData.Height);
        }
        else // We are moving in the y direction
        {
            txtData.SetBounds(txtData.Location.X, txtData.Location.Y + Variables.yDir, txtData.Width, txtData.Height);
        }
        await Task.Delay(intervalBetweenMovements);
    }
}

这样它将等待 move(x, y) 完成,然后再移动到下一行。

【讨论】:

  • 这正是我想要的。谢谢! :)
猜你喜欢
  • 2017-05-27
  • 1970-01-01
  • 1970-01-01
  • 2023-01-20
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多