我正在使用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 and Settings \ bruser \ My Documents \ Visual Studio 2010 \ Projects \ WindowsService \ Service1.cs 19 29 windowsservice
错误3找不到类型或命名空间名称'timer'(是吗? 缺少using指令或程序集引用?)C:\ Documents and Settings \ bruser \ My Documents \ Visual Studio 2010 \ Projects \ WindowsService \ Service1.cs 19 17 windowsservice
错误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 windowsservice
错误6当前名称'timer'不存在 上下文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 windowsservice
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上是否需要将命名空间命名为不同?
答案 0 :(得分:4)
你在这里缺少变量类型。
private timer = new Timer();
尝试:
private Timer timer = new Timer();
答案 1 :(得分:1)
您尚未定义timer
变量的类型。正确的语法是这样的:
private Timer timer = new Timer();
答案 2 :(得分:1)
您声明的变量timer
没有数据类型。
答案 3 :(得分:1)
可能还有其他错误,但您忘记输入计时器
private Timer timer = new Timer();