Reputation: 61
I Tried To Display SubTitles on the screen at the right time
now i got a exception
the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
class Program
{
private static Queue<Subtitle> _subtitles;
private static Subtitle _activeSubtitle;
private static TimeSpan _currentTime = new TimeSpan();
static void Main(string[] args)
{
_subtitles = new Queue<Subtitle>();
Subtitle title1 = new Subtitle()
{
StartTime = TimeSpan.Parse("00:00:06,000"),
EndTime = TimeSpan.Parse("00:00:07,400"),
Text = "Enjoy the movie!"
};
Subtitle title2 = new Subtitle()
{
StartTime = TimeSpan.Parse("00:00:07,500"),
EndTime = TimeSpan.Parse("00:00:09,500"),
Text = "Hi, my name is Mary"
};
Subtitle title3 = new Subtitle()
{
StartTime = TimeSpan.Parse("00:00:22,000"),
EndTime = TimeSpan.Parse("00:00:85,100"),
Text = "Hello my name is John."
};
_subtitles.Enqueue(title1);
_subtitles.Enqueue(title2);
_subtitles.Enqueue(title3);
Timer timer = new Timer(ShowSubtitles, null, 0, 100);
while (_currentTime <= new TimeSpan(0, 0, 0, 30))
{
}
Console.WriteLine("End");
}
private static void ShowSubtitles(object state)
{
if (_activeSubtitle == null && _subtitles.Count > 0)
_activeSubtitle = _subtitles.Dequeue();
Console.WriteLine(_currentTime);
if (_activeSubtitle != null)
{
if (_currentTime >= _activeSubtitle.StartTime && _currentTime <= _activeSubtitle.EndTime)
Console.WriteLine("\t{0}", _activeSubtitle.Text);
if (_currentTime >= _activeSubtitle.EndTime)
_activeSubtitle = null;
}
_currentTime = _currentTime.Add(new TimeSpan(0, 0, 0, 0, 100));
}
}
internal class Subtitle
{
public TimeSpan StartTime { get; set; }
public TimeSpan EndTime { get; set; }
public string Text { get; set; }
}
Exception:
Timer timer = new Timer(ShowSubtitles, null, 0, 100);
'System.Timers.Timer' does not contain a constructor that takes 4 arguments
Upvotes: 1
Views: 2634
Reputation: 1887
You need to either change your Timer to a System.Threading.Timer or redefine how you use the System.Timers.Timer in line with its definition at http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx.
It can get confusing as there are at least 3 types of Timer in C# - inside System.Timers, System.Threading and System.Windows.Forms!
Upvotes: 1
Reputation: 26737
System.Timers.Timer
doesn't have a constructor with four arguments
http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx
What you probably wanted was a System.Threading.Timer
http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx
Upvotes: 3