【问题标题】:errors using timer in C#在 C# 中使用计时器时出错
【发布时间】:2011-11-15 19:15:51
【问题描述】:

我正在使用 Microsoft Visual C# 2010 Express。我无法访问 Windows 服务模板,所以我在线下载了一个。我正在尝试在我的项目中实现计时器,但出现以下错误。

错误 1 ​​类、结构或接口成员中的无效标记“=” 声明 C:\Documents and Settings\bruser\My Documents\Visual Studio 2010\Projects\WindowsService\Service1.cs 19 23 windowsservice

错误 2 方法必须有一个返回类型 C:\Documents 和 设置\bruser\我的文档\Visual Studio 2010\Projects\WindowsService\Service1.cs 19 29 windowsservice

错误 3 找不到类型或命名空间名称“计时器”(您是 缺少 using 指令或程序集引用?) C:\Documents 和 设置\bruser\我的文档\Visual Studio 2010\Projects\WindowsService\Service1.cs 19 17 窗口服务

错误 4 名称 'timer' 在当前不存在 上下文 C:\Documents and Settings\bruser\My Documents\Visual Studio 2010\Projects\WindowsService\Service1.cs 23 4 windowsservice

错误 5 当前不存在名称“OnElapsedTime” 上下文 C:\Documents and Settings\bruser\My Documents\Visual Studio 2010\Projects\WindowsService\Service1.cs 23 45 窗口服务

Error 6 The name 'timer' does not exist in current 上下文 C:\Documents and Settings\bruser\My Documents\Visual Studio 2010\Projects\WindowsService\Service1.cs 24 13 windowsservice

错误 7 名称 'timer' 在当前不存在 上下文 C:\Documents and Settings\bruser\My Documents\Visual Studio 2010\Projects\WindowsService\Service1.cs 25 13 窗口服务

Service1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Timers; 

namespace SendFax
{
public partial class Service1: ServiceBase
{
    public Service1()
    {
        InitializeComponent();
    }

    private timer = new Timer();

    protected override void OnStart(string[] args)
    {
        timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
        timer.Interval = 30000; // every 30 seconds
        timer.Enabled = true;

    }

    protected override void OnStop()
    {
        // TODO: Add code here to perform any tear-down necessary to stop your service.
    }
    }
    }

Program.cs 和 Service1.cs 中的命名空间都命名为 $SaveNamespace$。因此,我将它们都更改为 SendFax。这两个文件应该有不同的命名空间名称吗?

我将 onElapsedTime 更改为 Tick。命名空间是否需要在 Program.cs 和 Service1.cs 上命名不同?

【问题讨论】:

  • 您能否在错误抱怨的行中添加注释(双击 VS Express 中的行,它会将您带到有问题的行)。猜测:private timer 的行。
  • 另外,你能用你获取Service1.cs的下载位置更新你的问题吗?
  • 你能修复一些更明显的错误,比如“方法必须有返回类型”,因为我不知道哪个是哪个。此外,OnElapsedTime 不是您要查找的 Timer 类的有效事件 Tick。这些都是粗心的错误......
  • 我从这个页面上的megaupload链接中得到了它:social.msdn.microsoft.com/Forums/en-US/vssetup/thread/…我怎样才能更正 OnElapsedTime 错误?
  • 阅读我的评论....您要订阅的事件是 Tick 而不是 OnElapsedTime 尝试使用您不理解的代码将总是出现这样的问题。

标签: c# service namespaces timer


【解决方案1】:

您在这里缺少变量类型。

private timer = new Timer();

试试:

private Timer timer = new Timer();

【讨论】:

  • 明白了!但现在我有了。错误 1 ​​类、结构或接口成员声明 C:\Documents and Settings\bruser\My Documents\Visual Studio 2010\Projects\WindowsService\Service1.cs 29 10 windowsservice 错误 2 类型或命名空间名称中的无效标记“void” Private' 找不到(您是否缺少 using 指令或程序集引用?) C:\Documents and Settings\bruser\My Documents\Visual Studio 2010\Projects\WindowsService\Service1.cs 29 2 windowsservice
  • 私人定时器 timer = new Timer(); protected override void OnStart(string[] args) { timer.Elapsed += new ElapsedEventHandler(OnElapsedTime); timer.Interval = 30000; // 每 30 秒 timer.Enabled = true; } Private void OnElapsedTime(object source, ElapsedEventArgs e) { // 在这里执行你的代码 }
  • 请更新您的问题.... 错误自行解释。关键字是 private 而不是 Private
  • 哦...我知道了。谢谢。我将开始更新我的问题。修复了 P。再次感谢。
【解决方案2】:

您尚未定义timer 变量的类型。正确的语法是这样的:

 private Timer timer = new Timer();

【讨论】:

    【解决方案3】:

    您声明的变量timer 没有数据类型。

    【讨论】:

      【解决方案4】:

      可能还有其他错误,但您忘记输入计时器

      private Timer timer = new Timer();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-16
        • 1970-01-01
        • 1970-01-01
        • 2020-03-26
        相关资源
        最近更新 更多