0

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;
        }
Ghasem
  • 14,455
  • 21
  • 138
  • 171
  • Why timer is not a desirable approach? – kennyzx Sep 16 '14 at 11:59
  • @kennyzx Cz in my project, I'll have to use many timers and I'm trying to reduce it by finding other ways. Timers can be a real headaches when I'm debugging the code. – Ghasem Sep 16 '14 at 13:41
  • The position is always changing, rapidly, there is no reasonable way the control can fire the event and keep everybody happy. So it doesn't. You know what to do about it. – Hans Passant Sep 16 '14 at 15:36

1 Answers1

1

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;
}
kennyzx
  • 12,845
  • 6
  • 39
  • 83