Click here to Skip to main content
15,888,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Get System.InvalidOperationException ("The calling thread can not access to the object, since the owner of this object is the other thread.") while trying to do animation.

C#
Thread t = new Thread(new ThreadStart(DoPointsShift));
t.Start();

private void DoPointsShift()
        {
            int turnsPerMinute = 5;
            long delay = 60 / turnsPerMinute * 1000 / (360 / 2);
            long deltaDelay = delay;

        int beginTime = Environment.TickCount;
        EasingFunctionBase ease = new CircleEase();
        ease.EasingMode = EasingMode.EaseInOut;
        while (true)
        {
            TimeSpan duration = TimeSpan.FromSeconds(1 - 1 * rnd.NextDouble());
            foreach (var p in points)
            {
                var x = p.OriginX - 50 + rnd.NextDouble() * 100;
                var y = p.OriginY - 50 + rnd.NextDouble() * 100;
                PointAnimation anim = new PointAnimation(new Point(x, y), duration);
                anim.EasingFunction = ease;
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    p.BeginAnimation(ParallaxPoint.PointCoordProperty, anim); //exception here
                }));
            }
            while (Environment.TickCount - beginTime < delay) { }
            delay += deltaDelay;
        }
    }

any ideas?
Posted
Comments
Sergey Alexandrovich Kryukov 27-Sep-15 23:17pm    
Your information is incomplete. What is, for example, points? And why are you trying to call this method in a separate thread? It does not seem to contain anything time-consuming. From the other hand, if you want to create a separate thread, you can do all animation directly in that thread, moving all objects you need in Dispatcher.Invoke...
—SA
Rajdeep Debnath 18-Dec-15 8:43am    
You need to pass p, anim as parameter

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900