C#显示子标题

时间:2012-06-13 13:02:00

标签: c#

我试图在适当的时间在屏幕上显示子标题
现在我有一个例外
代码:

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; }
}

例外:
定时器计时器=新的计时器(ShowSubtitles,null,0,100);
'System.Timers.Timer'不包含带有4个参数的构造函数

2 个答案:

答案 0 :(得分:3)

System.Timers.Timer没有带有四个参数的构造函数

http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx

您可能想要的是System.Threading.Timer

http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx

答案 1 :(得分:0)

您需要将Timer更改为System.Threading.Timer,或者根据http://msdn.microsoft.com/en-us/library/system.timers.timer.aspx的定义重新定义您如何使用System.Timers.Timer。

它可能会让人感到困惑,因为C#中至少有3种类型的Timer - 在System.Timers,System.Threading和System.Windows.Forms中!