事件只触发一次

时间:2013-09-30 19:18:40

标签: c# wpf events

我正在使用OxyPlot为简单的正弦运动设置动画:

       void ViewMotion()
    {
        foreach (File item in LoadedFiles())
        {
            LineSeries skeleton = new LineSeries();
            skeleton.MarkerType = MarkerType.Circle;
            skeleton.MarkerSize = 10;

            AnimationPlot.Series.Add(skeleton );

            var worker = new BackgroundWorker { WorkerSupportsCancellation = true };

            foreach (Channel node in item.ChannelAnim)
            {
                double x = 0;
                worker.DoWork += (s, e) =>
                {
                    while (!worker.CancellationPending)
                    {
                        x += 0.02;
                        if (skeleton.Points.Count > 0)
                        {
                            skeleton.Points.Remove(ss.Points.First());
                        }

                        skeleton.Points.Add(new DataPoint(50, 50 + (30 + x) * Math.Sin(x)));
                        AnimationPlot.RefreshPlot(true);
                        MessageBox.Show("");
                        Thread.Sleep(25);

                    }
                };

            }
            worker.RunWorkerAsync();


        }
    }

基本上只是一个点上下。 事件只触发一次的问题,所以我看不到动作而是初始位置。这不是一个有约束力的问题,这是事件的错误。我试过改变订单但没有成功。

顺便说一句,这是正常的: (上下两个点)

    ScatterSeries ss = new ScatterSeries();
    ScatterSeries ss2 = new ScatterSeries();
    public void TemporalMethod(object param)
    {
        ss.MarkerType = MarkerType.Circle;
        ss.MarkerSize = 20;
        ss.MarkerFill = OxyColors.Violet;
        MyModel.Series.Add(ss);

        ss2.MarkerType = MarkerType.Triangle;
        ss2.MarkerSize = 20;
        ss2.MarkerFill = OxyColors.Green;
        MyModel.Series.Add(ss2);

        var worker = new BackgroundWorker { WorkerSupportsCancellation = true };
        double x = 0;
        double y = 0;

        worker.DoWork += (s, e) =>
        {
            while (!worker.CancellationPending)
            {
                x += 0.02;
                y += 0.001;
                if (ss.Points.Count > 0)
                {
                    ss.Points.Remove(ss.Points.First());
                }
                if (ss2.Points.Count > 0)
                {
                    ss2.Points.Remove(ss2.Points.First());
                }

                ss.Points.Add(new DataPoint(50, 50 + (30 + x) * Math.Sin(x)));
                ss2.Points.Add(new DataPoint(55, 50 + 30 * Math.Sin(x + y)));
                MyModel.RefreshPlot(true);

                Thread.Sleep(25);
            }
        };

        worker.RunWorkerAsync();
    }
编辑:解决方案非常愚蠢,我误写了

                            if (skeleton.Points.Count > 0)
                        {
                            skeleton.Points.Remove(ss.Points.First());
                        }

而不是:

                            if (skeleton.Points.Count > 0)
                        {
                            skeleton.Points.Remove(skeleton.Points.First());
                        }

我不知道为什么,但我没有任何错误。

0 个答案:

没有答案