解决错误:'Timer'是'System.Windows.Forms.Timer'和'System.Timers.Timer'之间的模糊引用

时间:2012-05-01 14:21:12

标签: c#

我正在尝试创建一个Windows窗体应用程序,我想实现一个Timer。

  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'

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

3 个答案:

答案 0 :(得分:6)

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

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

答案 1 :(得分:5)

您可以使用类型别名消除歧义,例如:

using System.Timers;
using Timer = System.Timers.Timer;

答案 2 :(得分:2)

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

定时器没有出现在表单上,​​它们没有视觉效果。

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

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