Recently I created a windows service with System.Timers.Timer , this service will fulfill a task of creating a .txt file every minute (1 minute). p>
PROBLEM
After creating your Setup (installer) and can install it on my own PC or on another PC. The problem is that it does not fulfill its task of creating a .txt every minute. Someone who can correct me if it is the way to create a service with timer in the following structure.
STRUCTURE
CLASS:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace GEN_FE_WS
{
public partial class GEN_FE_WS : ServiceBase
{
Timer timer;
public GEN_FE_WS()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
this.timer = new System.Timers.Timer(1000D);//1000 MILISEGUNDOS O 1 MINUTO
this.timer.AutoReset = true;
this.timer.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_Elapsed);
this.timer.Start();
}
protected override void OnStop()
{
this.timer.Stop();
this.timer = null;
}
private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
ServiceCrear();
}
public static void ServiceCrear()
{
//Aquí creará el TXT
}
}
}
PROGRAM.CS:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace GEN_FE_WS
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new GEN_FE_WS()
};
ServiceBase.Run(ServicesToRun);
}
}
}
When you debug your Windows service, does it work and create the .txt file?
Yes, it only requires a bit of changes in the structure .
ADD - CLASS:
public void OnDebug()
{
OnStart(null);
}
EDITAMOS - PROGRAM.CS:
static void Main()
{
GEN_FE_WS myService = new GEN_FE_WS();
myService.OnDebug();
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
}
SOLUTION
I do not know the internal problems of my PC, I restart it and it works as expected. For that, I have put a new method to notify me by the Event Viewer what is happening with the Windows Service. Here they have it:
static void WriteEventLogEntry(string message)
{
// Create an instance of EventLog
System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog();
// Check if the event source exists. If not create it.
if (!System.Diagnostics.EventLog.SourceExists("Generador txt"))
{
System.Diagnostics.EventLog.CreateEventSource("Generador FAC.ELE.", "Application");
}
// Set the source name for writing log entries.
eventLog.Source = "Generador txt";
// Create an event ID to add to the event log
int eventID = 8;
// Write an entry to the event log.
eventLog.WriteEntry(message,
System.Diagnostics.EventLogEntryType.Warning,
eventID);
// Close the Event Log
eventLog.Close();
}
CONCLUSION
It already generates the .txt files every minute and I hope that this forum will serve as a guide for many who want to create a Windows Service Installer with Timer. Greetings and thanks to @Flxtr for their collaboration and dedication.