Event triggers only once

Oystein Bjorke 10 years ago 0
This discussion was imported from CodePlex

everytimer wrote at 2013-09-30 21:31:

I'm using OxyPlot for animating a simple sinusoidal motion:
    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();


        }
    }
Basically is just a dot going up and down. The problem that the event only triggers once, so I don't see a motion but the initial position. It's not a binding problem, it is something wrong with the event. I've tried changing the order but without success.

By the way, this is working correctly: (Two dots going up and down)
 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();
    }

everytimer wrote at 2013-09-30 21:50:

EDIT: The solution is pretty dumb, I mistakenly wrote
                            if (skeleton.Points.Count > 0)
                        {
                            skeleton.Points.Remove(ss.Points.First());
                        }
instead of:
                            if (skeleton.Points.Count > 0)
                        {
                            skeleton.Points.Remove(skeleton.Points.First());
                        }
I don't know why but I haven't got any errors.