Is there a way to update my label text by windows media player control current position live, without using a timer?
I know I can set the text by PositionChange
event. But it just works when you change the position manually and I couldn't find any event that can help me with my problem.
private void wmpPlayer_PositionChange(object sender, AxWMPLib._WMPOCXEvents_PositionChangeEvent e)
{
lblVoiceDuration.Text = wmpPlayer.Ctlcontrols.currentPositionString;
}
Since the label is updated periodically, a timer is inevitable. As long as the interval of the timer is not set too short (say, less than 100ms), and avoid putting too much work in the Tick event handler (reporting the current position of media takes little effort), it does no harm to the performance of the program.
If you just want to reduce the number of timers, however, you can use a background thread to update the label periodically. However, a WinForm timer is much easier to use, because it runs on UI thread you don't have to use BeginInvoke
to marshal the call back to UI thread.
Thread t = new Thread(new ThreadStart(UpdateLabelThreadProc));
t.Start();
bool isPlaying = false;
void UpdateLabelThreadProc()
{
while (isPlaying)
{
this.BeginInvoke(new MethodInvoker(UpdateLabel));
System.Threading.Thread.Sleep(1000);
}
}
private void UpdateLabel()
{
lblVoiceDuration.Text = wmpPlayer.Ctlcontrols.currentPositionString;
}