You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Crear Windows Services con Visual Studio 2013 o posteriores
Arquitectura de la solución en Visual Studio
Crear solución en blanco
Añadir un nuevo proyecto (Class Librery) a la solución capa lógica
Añadir un nuevo proyecto (Console) a la solución Testing
Añadir un nuevo proyecto (Windows Services) a la solución Servicio de Windows
Así se debe ver el proyecto
Clases a Usar
Libreria
namespaceLogical{publicclassLogueo{publicstringsourceFile{get;set;}=null;publicstringnameFile{get;set;}=null;publicstringWriteArchivo(stringmensaje){stringfile=null;try{System.IO.File.WriteAllText($"{this.sourceFile}{this.nameFile}.txt",mensaje);file=$"Se grabo el archivo {this.nameFile}.txt con exito";}catch(Exceptionex){file=$@"Error al generar el archivo: {System.Environment.NewLine} Detalle de error: {ex.Message}{System.Environment.NewLine} Error en {ex.Source}";}returnfile;}}}
namespaceLogical{publicclassClaseTest{publicstringnombres{get;set;}publicstringapellidos{get;set;}publicintedad{get;set;}publicstringmensaje{get;set;}publicClaseTest(string_nombre,string_apellidos,int_edad,string_mensaje){nombres=_nombre;apellidos=_apellidos;edad=_edad;mensaje=_mensaje;escribirArchivo();}publicstringescribirArchivo(){Logueolog=newLogueo(){nameFile=System.Configuration.ConfigurationManager.AppSettings[""].ToString(),sourceFile=getNameDocumentNow()};returnlog.WriteArchivo($@" Nombre: {nombres}{System.Environment.NewLine} Apellidos: {apellidos}{System.Environment.NewLine} Edad: {edad}{System.Environment.NewLine} Mensaje: {mensaje}{System.Environment.NewLine} Fecha: {DateTime.Now.ToString("ddMMyyyymmss")} ");}/// <summary>/// /// </summary>/// <returns></returns>/// <![CDATA[ /// Autor: Samuel Pilay Muñoz/// fecha creación: 2020-01-23 14:41:37/// ]]> publicstringgetNameDocumentNow(){return$"File_{DateTime.Now.ToString("ddMMyyyymmss")}";}}}publicpartialclassService1:ServiceBase{TimertmrMailer;bool_ProcesandoMailer;publicService1(){InitializeComponent();Doubleinterval=Double.Parse(ConfigurationSettings.AppSettings["intervalo_de_ejecucion"].ToString());tmrMailer=newTimer(interval);tmrMailer.Elapsed+=newSystem.Timers.ElapsedEventHandler(tmrMailer_Elapsed);}protectedoverridevoidOnStart(string[]args){// TODO: Add code here to start your service.tmrMailer.Enabled=true;_ProcesandoMailer=false;}protectedoverridevoidOnStop(){// TODO: Add code here to perform any tear-down necessary to stop your service.tmrMailer.Enabled=false;_ProcesandoMailer=true;}voidtmrMailer_Elapsed(objectsender,System.Timers.ElapsedEventArgse){this.process();}privatevoidprocess(){if(!_ProcesandoMailer){_ProcesandoMailer=true;try{ClaseTesthcont=newClaseTest("Samuel","Pilay Muñoz",30,"Esto es una clase");hcont.escribirArchivo();}catch(Exceptionex){throwex;}finally{_ProcesandoMailer=false;}}}}
Crear Windows Services Project Installer con Visual Studio 2013 o posteriores