WPF app struggling with dispatcher timer

frendly Nub 1 Reputation point
2021-04-21T10:01:07.577+00:00

I switched from windows forms to WPF recently and I made a simple four second timer. However, the timer looks like it is struggling, it has 100 milisecond interval, but when it tries to display value it just struggles, like display, stop for 0.2 seconds, display, stop etc. Here is my code:

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            prg.Height = 0;
            System.Windows.Threading.DispatcherTimer dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += dispatcherTimer_Tick;
            dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, 100);
            dispatcherTimer.Start();

            this.MouseDown += delegate { DragMove(); };
        }

private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            TimeSpan timeElapsed = DateTime.Now - startTime;
            time = timeElapsed.TotalSeconds.ToString("0.0");//Time elapsed to string
            label1.Content = time; //Display time elapsed on label
            secs = (float)Convert.ToDouble(time); //Convert from string to float for measure
            prg.Height = (secs / 4.0) * 117; //Height of "progress" bar
            if(secs >= 4.0) //If time elapsed more than 4 seconds reset the timer
            {
                startTime = DateTime.Now;
                // Imaging lower dont bother
                seq++;
                if (seq == 1)
                {
                    coei.Source = ImageSourceFromBitmap(Properties.Resources.convvice2);
                }
                if (seq == 2)
                {
                    coei.Source = ImageSourceFromBitmap(Properties.Resources.convvfire2);
                }
                if (seq == 3)
                {
                    coei.Source = ImageSourceFromBitmap(Properties.Resources.convvlight);
                }
                if (seq == 4)
                {
                    coei.Source = ImageSourceFromBitmap(Properties.Resources.convvphyd);
                }
                if (seq == 5)
                {
                    seq = 1;
                    coei.Source = ImageSourceFromBitmap(Properties.Resources.convvice2);
                }
            }
        }
Developer technologies Windows Presentation Foundation
{count} votes

1 answer

Sort by: Most helpful
  1. DaisyTian-1203 11,646 Reputation points
    2021-04-22T03:02:19.64+00:00

    I made some additions to the code you provided and did not reproduce your problem.
    Xaml part :

     <Grid>  
            <ProgressBar Name="prg" HorizontalAlignment="Left" Height="10" Margin="138,58,0,0" VerticalAlignment="Top" Width="100"/>  
            <Label Name="label1" Content="" HorizontalAlignment="Left" Margin="327,58,0,0" VerticalAlignment="Top" RenderTransformOrigin="5.441,-2.026"/>  
            <StackPanel Background="Azure" Height="100" Width="100">  
                <Image Name="coei" HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" Source="/paste.bmp"/>  
      
            </StackPanel>  
            <Label Name="lb2" Content="0" HorizontalAlignment="Left" Margin="502,59,0,0" VerticalAlignment="Top"/>  
      
        </Grid>  
    

    C# code:

      //....Your code Window_Loaded  
            DateTime startTime = DateTime.Now;  
            string time;  
            double secs;  
            int seq=0;  
            private void dispatcherTimer_Tick(object sender, EventArgs e)  
            {  
                    //....Your code  
                    coei.Stretch = Stretch.Fill;  
                    lb2.Content = seq;  
                }  
            }  
      
            public BitmapImage ImageSourceFromBitmap(string uri)  
            {  
                BitmapImage bi3 = new BitmapImage();  
                bi3.BeginInit();  
                bi3.UriSource = new Uri(uri, UriKind.RelativeOrAbsolute);  
                bi3.EndInit();  
                return bi3;  
            }  
    

    The result picture is:
    90140-2.gif

    Can you tell me which step I missed to reproduce your question?


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.