【发布时间】: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