0

I have trouble with animations. I have to control my double animation from asynchronous threads. Seperate threads will change uklepe value asynchronously, animations will play as a result of changes. That's what i tried but i am still getting this error even i used Dispatcher :

The Calling Thread Cannot Access This Object Because A Different Thread Owns It

    protected bool üstklepe = false;
    public bool uklepe
    {
        get
        {
            return üstklepe;
        }
        set
        {
            if (üstklepe == false && value == true)
            {
                var da = new DoubleAnimation(0, 90, new Duration(TimeSpan.FromMilliseconds(100)));
                var rt = new RotateTransform(0, 0, 0);
               Dispatcher.Invoke(() => {Ustklepe.RenderTransform = rt;});
                Dispatcher.Invoke(() => {Ustklepe.RenderTransformOrigin = new Point(0, 0);});
                 Dispatcher.Invoke(() => {rt.BeginAnimation(RotateTransform.AngleProperty, da);});

                RadialGradientBrush myBrush = new RadialGradientBrush();
                myBrush.GradientOrigin = new Point(0.35, 0.65);
                myBrush.GradientStops.Add(new GradientStop(Colors.White, 0.0));
                myBrush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FF91D393"), 0.916));

                //SolidColorBrush Brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF91D393"));
                Dispatcher.Invoke(() => { Ustklepe.Fill = myBrush; });



                üstklepe = value;

            }
            else if (üstklepe == true && value == false)
            {
                var da = new DoubleAnimation(90, 0, new Duration(TimeSpan.FromMilliseconds(100)));
                var rt = new RotateTransform(0, 0, 0);
                 Dispatcher.Invoke(() => {Ustklepe.RenderTransform = rt;});
                 Dispatcher.Invoke(() => {Ustklepe.RenderTransformOrigin = new Point(0, 0);});
                 Dispatcher.Invoke(() => {rt.BeginAnimation(RotateTransform.AngleProperty, da);});


                RadialGradientBrush myBrush = new RadialGradientBrush();
                myBrush.GradientOrigin = new Point(0.35, 0.65);
                myBrush.GradientStops.Add(new GradientStop(Colors.White, 0.0));
                myBrush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FFFF2F33"), 0.916));

                //SolidColorBrush Brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFF2F33"));
                Dispatcher.Invoke(() => { Ustklepe.Fill = myBrush; });



                üstklepe = value;

            }
            else
                üstklepe = value;

        }
    }
M.Balta
  • 9
  • 3
  • sorry, i put wrongly . now edited. thats the final version of my code but i am still getting this error from here: Dispatcher.Invoke(() => {rt.BeginAnimation(RotateTransform.AngleProperty, da);}); – M.Balta Jan 26 '19 at 05:42
  • All objects derived from DispatcherObject must be be created in the same thread. – Clemens Jan 26 '19 at 10:03

1 Answers1

0

I think the problem is even though you are setting the rotate transform with dispatcher you are creating the instance in the thread which sets the uklepe property with these lines.

        var da = new DoubleAnimation(0, 90, new Duration(TimeSpan.FromMilliseconds(100)));
        var rt = new RotateTransform(0, 0, 0);

Edit: And these lines too

        RadialGradientBrush myBrush = new RadialGradientBrush();
        myBrush.GradientOrigin = new Point(0.35, 0.65);
        myBrush.GradientStops.Add(new GradientStop(Colors.White, 0.0));
        myBrush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FFFF2F33"), 0.916));

You need to run these lines inside the UI thread too

Mertus
  • 1,145
  • 11
  • 17