C# C语言中的异步方法调用#

C# C语言中的异步方法调用#,c#,wpf,asynchronous,storyboard,C#,Wpf,Asynchronous,Storyboard,我们有这样的情况。我们有一张画布,上面画着大量的人物。它可能是1个或更多(例如千个),我们需要使用故事板将它们的翻译动画化到另一个位置(点击按钮): internal void someStoryBoard(figure someFigure, double coordMoveToValue) { string sbName = "StoryBoard_" + figure.ID; string regName = "figure_" + figure.ID; try

我们有这样的情况。我们有一张画布,上面画着大量的人物。它可能是1个或更多(例如千个),我们需要使用故事板将它们的翻译动画化到另一个位置(点击按钮):

internal void someStoryBoard(figure someFigure, double coordMoveToValue)
{
    string sbName = "StoryBoard_" + figure.ID;
    string regName = "figure_" + figure.ID;
    try 
    {
        cnvsGame.Resources.Remove(sbName);
        cnvsGame.UnregisterName(regName);
    } 
    catch{ }
        someCanvas.RegisterName(regName, someFigure.Geometry);
        var moveFigureYAnimation = new PointAnimation();
        moveFigureYAnimation.From = new Point(someFigure.Geometry.Center.X, someFigure.Geometry.Center.Y);
        moveFigureYAnimation.To = new Point(someFigure.eGeometry.Center.X, coordMoveToValue);
        moveFigureYAnimation.Duration = TimeSpan.FromSeconds(0.5);
        var sbFigureMove = new Storyboard();
        Storyboard.SetTargetName(sbFigureMove, regName);
        Storyboard.SetTargetProperty(sbFigureMove, new PropertyPath(Geometry.CenterProperty));
        sbFigureMove.Children.Add(moveFigureYAnimation);
        cnvsGame.Resources.Add(sbName, sbFigureMove);
        sbFigureMove.Begin();           
    }
数字存储在列表中。我们使用for循环将此故事板称为:

for(int i = 0; i<listOfFigures.Count; i++)
    {
        someStoryBoard(listOfFigures[i], someCoord);
    }

for(int i=0;i)当调用某个故事板时,它立即开始移动图形,而不是等待整个循环完成。

您可以通过调用
Dispatcher.InvokeAsync
将操作添加到调度程序队列中。您还可以根据需要指定调度程序优先级


请注意,移动数千个项目的速度不可能很快,因此您可能需要重新考虑绘图逻辑。即使启动动画速度很慢,也很可能动画速度不够快。

您可以尝试使用async/Wait modifier

async internal Task someStoryBoard(figure someFigure, double coordMoveToValue)