WPF Dispatcher Timer issue

Mar 23, 2012 at 10:50 PM

Hi All,

 

Thanks for making this powerfull library avaiable.

I am a newbee in C#, but I gave myself the challange to learn wpf more by trying to convert Audio Playback demo. I succeed by being able to select the output driver model and select the output and then play-pause-stop a file, volume is ok as well. I used the Dispatcher Timer for the elapsed seconds label control and the slider. They work very well but I am hearing a kind of "tick" at my time intervals when I play a MP3. I thought this was heavy work for the dispatcher timer and try to use System.Timers. However since this is a different thread then the gui, I am stuck here reading through google for hours but still could not find a way to change my tick event below for the Gui to be invoked; Can you please help?

private void TickTimer()
			{
			if (waveOut != null && fileWaveStream != null)
				{
				TimeSpan currentTime = ( waveOut.PlaybackState == PlaybackState.Stopped ) ? TimeSpan.Zero : fileWaveStream.CurrentTime;
				if (fileWaveStream.Position >= fileWaveStream.Length)
					{
					sliderSeconds.Value = 0;
					labelSeconds.Content = "";
					//buttonStop.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
					buttonNext.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
					}
				else
					{
					sliderSeconds.Value = (int)currentTime.TotalSeconds;
					labelSeconds.Content = String.Format("{0:00}:{1:00}/{2:00}:{3:00}", (int)currentTime.TotalMinutes,
						currentTime.Seconds, (int)fileWaveStream.TotalTime.TotalMinutes, fileWaveStream.TotalTime.Seconds);
					}
				}
			else
				{
				sliderSeconds.Value = 0;
				}
			}

Coordinator
Mar 26, 2012 at 9:55 AM

In WPF, to run something on the GUI thread, you need to use BeginInvoke on the Dispatcher. You can get the Dispatcher off any control on your form.