Process Error using Timers

5 posts, 1 answers
  1. Ramiro
    Ramiro avatar
    46 posts
    Member since:
    Aug 2010

    Posted 01 Feb 2011 Link to this post

    Hi!


    I'm having my UserControl to show Thumbnails (list of images) using transition control. Image changes one to another every 7 sec. and to achieve this I'm using a Timer. Next is a snipped code:

    public partial class ctlPublicidad : UserControl
    {
    Timer m_objTemporizador = new Timer();
    List<string> m_vaThumbNails = new List<string>();
     
    public ctlPublicidad()
            {
    this.InitializeComponent();
    IndiceThumbnailActivo = -1;
    }
     
    // This is execute when UserControl is loaded
    private void UserControl_Loaded(object sender, RoutedEventArgs e)
            {
    MostrarSiguienteImagenThumbNail();
                                    m_objTemporizador = new Timer(7000);
                                    m_objTemporizador.Elapsed += new ElapsedEventHandler(Temporizador_Elapsed);
                                    m_objTemporizador.Start();
    }
     
    void Temporizador_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
            {
                MostrarSiguienteImagenThumbNail();
            }
     
           private void MostrarSiguienteImagenThumbNail()
            {
                Random vnNumeroRandom = new Random();
                int vnEfecto = vnNumeroRandom.Next(1, 7);
                switch (vnEfecto)
                {
                    case 1: TransitionControl.Transition = new MotionBlurredZoomTransition();
                            TransitionControl.Duration = new TimeSpan(0, 0, 0, 0, 400);
                            break;
                    case 2: TransitionControl.Transition = new FadeTransition();
                            TransitionControl.Duration = new TimeSpan(0, 0, 0, 0, 750);
                            break;
                    case 3: TransitionControl.Transition = new LinearFadeTransition() { Angle = 0.3, Darker = 0.3, Thickness = 0.2 };
                            TransitionControl.Duration = new TimeSpan(0, 0, 0, 0, 750);
                            break;
                    case 4: TransitionControl.Transition = new WaveTransition() { Angle = 0.5, Amplitude = 0.15 };
                            TransitionControl.Duration = new TimeSpan(0, 0, 0, 0, 750);
                            break;
                    case 5: TransitionControl.Transition = new FlipWarpTransition();
                            TransitionControl.Duration = new TimeSpan(0, 0, 0, 0, 750);
                            break;
                    case 6: TransitionControl.Transition = new SlideAndZoomTransition();
                            TransitionControl.Duration = new TimeSpan(0, 0, 0, 0, 750);
                            break;
                    case 7: TransitionControl.Transition = new RollTransition();
                            TransitionControl.Duration = new TimeSpan(0, 0, 0, 0, 750);
                            break;
                }
     
                TransitionControl.Content = new Image() { Source = new BitmapImage(new Uri(m_vaThumbNails[++IndiceThumbnailActivo], UriKind.RelativeOrAbsolute)) };
            }
    }

    First image is showed fine, but when timer elapsed is reached, exception is thrown when try to change to another transition effect. Error is:
    "The calling thread can not access this object because the owner is another thread". How can I do to achieve this thumbnail???


    thanks
  2. Ramiro
    Ramiro avatar
    46 posts
    Member since:
    Aug 2010

    Posted 02 Feb 2011 Link to this post

    Hi!

    I forgot!, UserControl is used in a Windows Application
  3. Answer
    George
    Admin
    George avatar
    1332 posts

    Posted 07 Feb 2011 Link to this post

    Hello Ramiro,

     
    I would suggest you to use the DispatcherTimer class, instead of Timer.

    I hope this helps! 

    Kind regards,
    George
    the Telerik team
    Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
  4. Ramiro
    Ramiro avatar
    46 posts
    Member since:
    Aug 2010

    Posted 07 Feb 2011 Link to this post

    Hi!


    Can you help me by using DispatcherTimer class sample, thanks
  5. Ramiro
    Ramiro avatar
    46 posts
    Member since:
    Aug 2010

    Posted 09 Feb 2011 Link to this post

    I've found solution as you told me, thanks very much for your help!!
     
Back to Top