2

我正在尝试创建一个 Windows 窗体应用程序,并且我想实现一个计时器。

  public void timerStart()
    {
        DateTime now = DateTime.Now;
        DateTime finish = base.taskEndDate;
        finish = finish.AddHours(_appointmentTime.Hours); //_appointmentTime is a user defined value.
        finish = finish.AddMinutes(_appointmentTime.Minutes);
        finish = finish.AddMilliseconds(_appointmentTime.Milliseconds);

        //Calculating the milliseconds left till task ends.
        int daysLeft = finish.Day - now.Day;
        int hoursLeft = finish.Hour - now.Hour;
        int minsLeft = finish.Minute - now.Minute;
        int secLeft = finish.Second - now.Second;
        int milLeft = finish.Millisecond - now.Millisecond;

        //Preparing to Start the timer.
        TimeSpan end = new TimeSpan(daysLeft, hoursLeft, minsLeft, secLeft, minsLeft);
        MessageBox.Show(end.ToString());
        double x = end.TotalMilliseconds;
        System.Timers.Timer _timer = new Timer(x);          

    }

是我如何定义我的方法但我收到错误

 'Timer' is an ambiguous reference between 'System.Windows.Forms.Timer' and 'System.Timers.Timer'

而且我不确定如何解决该错误。一旦计时器结束,我计划制作一个提醒用户的事件。我不希望计时器出现在表单上。

4

3 回答 3

6

您似乎添加了两个using引用(System.Windows.FormsSystem.Timers)。所以要么删除一个或完全限定类型名称:

System.Timers.Timer _timer = new System.Timers.Timer(x);  
于 2012-05-01T14:23:05.350 回答
5

您可以使用类型别名来消除它们的歧义,如下所示:

using System.Timers;
using Timer = System.Timers.Timer;
于 2012-05-01T14:26:21.793 回答
2

System.Timers.Timer _timer = new System.Timers.Timer(x);

计时器不会出现在表单上,​​它们不是可视的。

还要记住 DNF 中(三种!)不同类型的计时器之间的差异。

http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

于 2012-05-01T14:23:06.893 回答