I am using Microsoft Visual C# 2010 Express. I did not have access to a Windows Services template so I downloaded one online. I am trying to implement a timer into my project and I am getting the following errors.
Error 1 Invalid token ‘=’ in class, struct, or interface member
declaration C:\Documents and Settings\bruser\My Documents\Visual
Studio 2010\Projects\WindowsService\Service1.cs 19 23 windowsserviceError 2 Method must have a return type C:\Documents and
Settings\bruser\My Documents\Visual Studio
2010\Projects\WindowsService\Service1.cs 19 29 windowsserviceError 3 The type or namespace name ‘timer’ could not be found (are you
missing a using directive or an assembly reference?) C:\Documents and
Settings\bruser\My Documents\Visual Studio
2010\Projects\WindowsService\Service1.cs 19 17 windowsserviceError 4 The name ‘timer’ does not exist in the current
context C:\Documents and Settings\bruser\My Documents\Visual Studio
2010\Projects\WindowsService\Service1.cs 23 4 windowsserviceError 5 The name ‘OnElapsedTime’ does not exist in the current
context C:\Documents and Settings\bruser\My Documents\Visual Studio
2010\Projects\WindowsService\Service1.cs 23 45 windowsserviceError 6 The name ‘timer’ does not exist in the current
context C:\Documents and Settings\bruser\My Documents\Visual Studio
2010\Projects\WindowsService\Service1.cs 24 13 windowsserviceError 7 The name ‘timer’ does not exist in the current
context 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.
}
}
}
The namespace in both Program.cs and Service1.cs were named $SaveNamespace$. So, I changed both of them to SendFax. Should these two files have separate namespace names?
I changed onElapsedTime to Tick. Do the namespaces need to be named different on Program.cs and Service1.cs?
You are missing the variable type here.
Try: