So, I was creating a windows service and i was following this video to setup the windows service.
https://www.youtube.com/watch?v=tAF9krJu3cs
So, I have a Model, I get the Model in the controller so I can build what I want to send, then, I call that Controller on the Service. After that, I created the Installer. Problem: I have 3 and i was able to create them adding: [RunInstaller(true)] in the beggining of every service. After that, I was saying that they were not manual and "prepraring" them for the errors, starting them, and, after that I started them. But, I turned on one of them, the other turns off, and so on. I was able to turn the 3 on after a while (maybe this was a bug) but they all stopped working again..... My code:
ProjectInstaller:
enter image description here
ProjectInstaller.Designer.cs :
{
partial class ProjectInstaller
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
this.AccountService = new System.ServiceProcess.ServiceInstaller();
this.FinInfoPrevYearService = new System.ServiceProcess.ServiceInstaller();
this.GetFinancialInfoService = new System.ServiceProcess.ServiceInstaller();
//
// serviceProcessInstaller1
//
this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;
this.serviceProcessInstaller1.Password = null;
this.serviceProcessInstaller1.Username = null;
this.serviceProcessInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceProcessInstaller1_AfterInstall);
//
// AccountService
//
this.AccountService.ServiceName = "AccountService";
this.AccountService.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.AccountService_AfterInstall);
//
// FinInfoPrevYearService
//
this.FinInfoPrevYearService.ServiceName = "FinInfoPrevYearService";
this.FinInfoPrevYearService.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.FinInfoPrevYearService_AfterInstall);
//
// GetFinancialInfoService
//
this.GetFinancialInfoService.ServiceName = "GetFinancialInfoService";
this.GetFinancialInfoService.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.GetFinancialInfoService_AfterInstall);
//
// ProjectInstaller
//
this.Installers.AddRange(new System.Configuration.Install.Installer[] {
this.serviceProcessInstaller1,
this.AccountService,
this.FinInfoPrevYearService,
this.GetFinancialInfoService});
}
#endregion
private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
private System.ServiceProcess.ServiceInstaller AccountService;
private System.ServiceProcess.ServiceInstaller FinInfoPrevYearService;
private System.ServiceProcess.ServiceInstaller GetFinancialInfoService;
}
}
ProjectInstaller.cs:
{
[RunInstaller(true)]
public partial class ProjectInstaller : System.Configuration.Install.Installer
{
public ProjectInstaller()
{
InitializeComponent();
}
private void serviceProcessInstaller1_AfterInstall(object sender, InstallEventArgs e)
{
}
private void AccountService_AfterInstall(object sender, InstallEventArgs e)
{
}
private void GetFinancialInfoService_AfterInstall(object sender, InstallEventArgs e)
{
}
private void FinInfoPrevYearService_AfterInstall(object sender, InstallEventArgs e)
{
}
}
}
One of the services:
enter image description here
Please help me guys.. I dont know what to do anymore...
Related
I have read through a few threads here, but have not come to any conclusion.
An asynchronous method in another class is to be executed by button click.
In this method the current state should be added again and again in a text field of the form:
public interface MainForm {
string LogText { get; set; }
}
public partial class KatalogForm : Form, MainForm {
public string LogText {
get { return rtbxLog.Text; }
set { rtbxLog.Text += value; }
}
private void btnCreateCatalogues_Click(object sender, EventArgs e) {
Task.Run(() => catalogues.Create());
}
}
Excerpt from the second class:
private static MainForm mainForm;
public async void Create() {
//Stuff
//Update TextBox
}
Since the method is asynchronous, I can't access the textbox directly here.
I know that I have to work with Invoke here, but I can't implement this properly.
What is the best way to solve this?
Usually, when I have to asynchronously update a TextBox with data from external sources (ex: an operations log) I use a queue and a timer (Windows.Forms).
The method called by external objects adds the new data.
The timer clears pending data at a reasonable rate and updates the text box.
The advantages are:
Fast requests: The update method invoked by external sources only add items to the queue.
Thread safe: The text box is only updated within its own UI thread, so no cross-thread errors can occur.
Less allocations: No Task or BeginInvoke calls are required, avoiding allocation of temporary objects.
Few UI updates: The items are applied to the text box as batches (multiple items at the same time) based on the timer frequency.
First-in/First-out: No risk of overlap of items due to asynchronous operations scheduled and executed in the wrong order.
See the sample class (LogBox user control) below, split into LogBox.cs and LogBox.Designer.cs:
Other objects would call Log method.
The default rate is 100ms; that is, 10 times a second (for a human reader should be enough).
Sample code (LogBox.cs):
using System;
using System.Collections.Concurrent;
using System.Windows.Forms;
namespace SAMPLE
{
[Docking(DockingBehavior.Ask)]
public partial class LogBox : UserControl
{
private readonly ConcurrentQueue<string> PendingLog = new ConcurrentQueue<string>();
public LogBox()
{
InitializeComponent();
}
private void tmrLog_Tick(object sender, EventArgs e) { this.ProcessPendingLog(); }
private void ProcessPendingLog()
{
if (!this.Disposing && !this.IsDisposed && this.IsHandleCreated)
try
{
if (!this.PendingLog.IsEmpty)
{
string item;
while (this.PendingLog.TryDequeue(out item))
{
txtLog.AppendText(item);
}
}
}
catch (Exception ex) { /* ... */ }
}
public void Log(string text) { this.PendingLog.Enqueue(text); }
}
}
Sample code (LogBox.Designer.cs):
namespace SAMPLE
{
partial class LogBox
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.tmrLog = new System.Windows.Forms.Timer(this.components);
this.txtLog = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// tmrLog
//
this.tmrLog.Enabled = true;
this.tmrLog.Tick += new System.EventHandler(this.tmrLog_Tick);
//
// txtLog
//
this.txtLog.Dock = System.Windows.Forms.DockStyle.Fill;
this.txtLog.Location = new System.Drawing.Point(0, 0);
this.txtLog.Multiline = true;
this.txtLog.Name = "txtLog";
this.txtLog.ReadOnly = true;
this.txtLog.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this.txtLog.Size = new System.Drawing.Size(200, 200);
this.txtLog.TabIndex = 0;
//
// LogBox
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.txtLog);
this.Name = "LogBox";
this.Size = new System.Drawing.Size(200, 200);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Timer tmrLog;
private System.Windows.Forms.TextBox txtLog;
}
}
I would recommend use IProgress<string> for catalogues.Create()
Task.Run(async () =>
{
var createProgress = new Progress<string>(ReportProgress);
await catalogues.Create(createProgress);
});
void ReportProgress(string reportMessage)
{
//update log here
}
usage inside Create
async Task Create(IProgress<string> progress)
{
foreach (var category in categories)
{
// some staff
progress.Report($"{category} - completed");
}
}
IProgress example
When I run the following code :
static void Main(string[] args)
{
if (!System.Diagnostics.EventLog.SourceExists("eventSource"))
{
System.Diagnostics.EventLog.CreateEventSource("eventSource", "");
}
System.Diagnostics.EventLog eventLog = new System.Diagnostics.EventLog();
eventLog.Source = "eventSource";
eventLog.Log = "";
for (int i = 0; i < 10000; i++)
{
Thread.Sleep(100);
eventLog.WriteEntry("test", EventLogEntryType.Information);
//I also tried the static method, the program still leaks
//System.Diagnostics.EventLog.WriteEntry("eventSource", "test", EventLogEntryType.Information);
}
Console.ReadKey();
}
The memory usage starts around 1MB, but rise up very quickly and doesn't stop. Why ?
The code I'm using to install windows service with a designated event log:
[RunInstaller(true)]
public partial class SampleServiceInstaller : System.Configuration.Install.Installer
{
private string SampleServiceName = string.Empty;
private string SampleLogName = string.Empty;
public SampleServiceInstaller()
{
this.SampleServiceName = "SampleService";
this.SampleLogName = this.SampleServiceName + "Log";
ServiceProcessInstaller serviceProcessInstaller = new ServiceProcessInstaller();
serviceProcessInstaller.Password = null;
serviceProcessInstaller.Username = null;
serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
ServiceInstaller serviceInstaller = new ServiceInstaller();
//This must be identical to the WindowsService.ServiceBase name
// set in the constructor of WindowsService.cs
serviceInstaller.ServiceName = this.SampleServiceName;
serviceInstaller.DisplayName = this.SampleServiceName;
serviceInstaller.StartType = ServiceStartMode.Automatic;
serviceInstaller.Description = "Sample Windows Service";
// kill the default event log installer
serviceInstaller.Installers.Clear();
// Create Event Source and Event Log
// This recreates the log every time the service is reinstaled (old messages are lost)
if (EventLog.SourceExists(this.SampleServiceName)) EventLog.DeleteEventSource(this.SampleServiceName);
System.Diagnostics.EventLogInstaller logInstaller = new System.Diagnostics.EventLogInstaller();
logInstaller.Source = this.SampleServiceName; // use same as ServiceName
logInstaller.Log = this.SampleLogName; //can't be the same as service name
// Add all installers
this.Installers.AddRange(new Installer[] {
serviceProcessInstaller, serviceInstaller, logInstaller
});
}
public override void Install(System.Collections.IDictionary savedState)
{
base.Install(savedState);
}
protected override void OnBeforeUninstall(System.Collections.IDictionary savedState)
{
base.OnBeforeUninstall(savedState);
}
service code:
public partial class SampleService : ServiceBase
{
/// <summary>
/// Designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Event log for the service
/// </summary>
EventLog serviceLog;
/// <summary>
/// Public Constructor for WindowsService.
/// - Initialization code here.
/// </summary>
public SampleService()
{
InitializeComponent();
}
/// <summary>
/// The Main Thread: list of services to run.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new SampleService() };
ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Startup code
/// </summary>
/// <param name="args"></param>
protected override void OnStart(string[] args)
{
base.OnStart(args);
//
// run your own start code here
//
serviceLog.WriteEntry("My service started", EventLogEntryType.Information, 0);
}
/// <summary>
/// Stop code
/// </summary>
protected override void OnStop()
{
//
// run your own stop code here
//
serviceLog.WriteEntry("My service stopped", EventLogEntryType.Information, 0);
base.OnStop();
}
/// <summary>
/// Cleanup code
/// </summary>
/// <param name="disposing"></param>
protected override void Dispose(bool disposing)
{
//
// do disposing here
//
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// Pause code
/// </summary>
protected override void OnPause()
{
base.OnPause();
//
// code to run if service pauses
//
}
/// <summary>
/// Continue code
/// </summary>
protected override void OnContinue()
{
base.OnContinue();
//
// code tu run when service continues after being paused
//
}
/// <summary>
/// Called when the System is shutting down
/// - when special handling
/// of code that deals with a system shutdown, such
/// as saving special data before shutdown is needed.
/// </summary>
protected override void OnShutdown()
{
//
// code tu run when system is shut down
//
base.OnShutdown();
}
/// <summary>
/// If sending a command to the service is needed
/// without the need for Remoting or Sockets,
/// this method is used to do custom methods.
/// int command = 128; //Some Arbitrary number between 128 & 256
/// ServiceController sc = new ServiceController("NameOfService");
/// sc.ExecuteCommand(command);
/// </summary>
/// <param name="command">Arbitrary Integer between 128 & 256</param>
protected override void OnCustomCommand(int command)
{
base.OnCustomCommand(command);
//
// handle custom code here
//
}
/// <summary>
/// Useful for detecting power status changes,
/// such as going into Suspend mode or Low Battery for laptops.
/// </summary>
/// <param name="powerStatus">The Power Broadcast Status
/// (BatteryLow, Suspend, etc.)</param>
protected override bool OnPowerEvent(PowerBroadcastStatus powerStatus)
{
//
// handle power events here
//
return base.OnPowerEvent(powerStatus);
}
/// <summary>
/// To handle a change event
/// from a Terminal Server session.
/// Useful if determining
/// when a user logs in remotely or logs off,
/// or when someone logs into the console is needed.
/// </summary>
/// <param name="changeDescription">The Session Change
/// Event that occured.</param>
protected override void OnSessionChange(SessionChangeDescription changeDescription)
{
//
// handle session change here
//
base.OnSessionChange(changeDescription);
}
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
// first 8 letters should be unique
this.ServiceName = "SampleService";
// if you want to log service event to log registered while installing the service
string newLogName = this.ServiceName + "Log";
string newSourceName = this.ServiceName;
if (!EventLog.SourceExists(newSourceName))
{
EventLog.CreateEventSource(newSourceName, newLogName);
}
serviceLog = new EventLog();
serviceLog.Source = newSourceName;
serviceLog.Log = newLogName;
// Causes log to be disposed when the service is disposed
components.Add(serviceLog);
// Flags set whether or not to handle that specific type of event.
this.CanHandlePowerEvent = true;
this.CanHandleSessionChangeEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
this.CanStop = true;
}
}
I currently have a program to install correctly as a service but the Startup Type is set to "Manual". How do I make this application set Startup Type = Automatic?
static void Main(string[] args) {
if (System.Environment.UserInteractive) {
if (args.Length > 0) {
switch (args[0]) {
case "/install": {
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
break;
}
case "/uninstall": {
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
break;
}
}
}
} else {
if (!Environment.UserInteractive) {
// running as service
using (var service = new DocLogicJMS())
ServiceBase.Run(service);
} else {
// running as console app
Start(args);
Console.WriteLine("Press any key to stop...");
Console.ReadKey(true);
Stop();
}
}
}
And JMS is:
namespace JMS {
partial class DocLogicJMS {
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
this.ServiceName = "DocLogic JMS";
}
#endregion
}
}
It seems this question has been answered here already:
How do I change a Windows Service's startup type in .NET (post-install)?
This seems to be the most upvoted answer:
var svc = new ServiceController("ServiceNameGoesHere");
ServiceHelper.ChangeStartMode(svc, ServiceStartMode.Automatic);
I am learning window service from msdn link :
http://msdn.microsoft.com/en-us/library/zt39148a(v=vs.110).aspx
I have properly created , install first Time ....when I try to start it from Service.msc ..it is throwing Error :
Error 1053 service didnot respond to start or control request
this is my Code :
public partial class ASMSService : ServiceBase
{
private Timer myTimer;
TimeSpan setTime;
private DateTime previousDate;
private DateTime todaysDate;
public ASMSService()
{
InitializeComponent();
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource", "MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
protected override void OnStart(string[] args)
{
myTimer = new System.Threading.Timer(new TimerCallback(TimerAction1));
SetTimer(11, 07, 00);
}
protected override void OnStop()
{
}
private void SetTimer(int hours, int minutes, int seconds)
{
todaysDate = DateTime.Today;
previousDate = todaysDate.AddDays(-1);
setTime = todaysDate.AddHours(hours).AddMinutes(minutes).AddSeconds(seconds).TimeOfDay; ;
}
private void TimerAction1(object e)
{
//some Code
}
}
this is design Code
partial class ASMSService
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.eventLog1 = new System.Diagnostics.EventLog();
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).BeginInit();
//
// ASMSService
//
this.ServiceName = "ASMSService";
((System.ComponentModel.ISupportInitialize)(this.eventLog1)).EndInit();
}
#endregion
private System.Diagnostics.EventLog eventLog1;
}
this is Program class:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new ASMSService()
};
ServiceBase.Run(ServicesToRun);
}
}
I have read similar posts on this ..Some posts suggest to install microsoft Patch ..Other suggest the object created should be disposed properly ..I also tried to do that in Onstop method..But it is not working ..Some posts suggest that ServiceBase.Run() method should be called in Main() method ...it is also present in My code
Please Suggest
I can see one great problem with the ASMSService's timer:
It is created :
myTimer = new System.Threading.Timer(new TimerCallback(TimerAction1));
But it is never started:
private void SetTimer(int hours, int minutes, int seconds)
{
todaysDate = DateTime.Today;
previousDate = todaysDate.AddDays(-1);
setTime = todaysDate.AddHours(hours).AddMinutes(minutes).AddSeconds(seconds).TimeOfDay; ;
// You have set the setTime field, otherwise the timer will still have the infinite dueTime and interval - it is not running at all
// You should call SetChange method to start it.
this.mytimer.SetChange(0, (Int64)setTime.ToMilliseconds());
}
SetChange method is required to start the timer if you use the simplest constructor
You may also want to read the following links, they are dealing with similar situations and provide few insights to consider:
Windows service with timer
System.Threading.Timer Not Starting?
I have the following code for a windows service project. I have successfully built it and installed it. When I start it I get an event started in the event log. However, I never get the event for "In Onstart" any idea why this is going on?
namespace ADServiceCarlos
{
public partial class ADServiceCarlos : ServiceBase
{
public ADServiceCarlos()
{
InitializeComponent();
this.AutoLog = true;
if (!System.Diagnostics.EventLog.SourceExists("MySource"))
{
System.Diagnostics.EventLog.CreateEventSource(
"MySource","MyNewLog");
}
eventLog1.Source = "MySource";
eventLog1.Log = "MyNewLog";
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("In OnStart");
}
protected override void OnStop()
{
}
}
}
OK, so this may solve your problem. It's hard to tell exactly without being able to see all your code, but read this - more specifically the "caution" part.
Do not use the constructor to perform processing that should be in
OnStart. Use OnStart to handle all initialization of your service. The
constructor is called when the application's executable runs, not when
the service runs. The executable runs before OnStart. When you
continue, for example, the constructor is not called again because the
SCM already holds the object in memory. If OnStop releases resources
allocated in the constructor rather than in OnStart, the needed
resources would not be created again the second time the service is
called.
So everything you are doing to initialise the event log in your constructor should be moved to the OnStart event. This will ensure it is create properly each time the service start, which means you should be able to log your OnStart event correctly (providing you do it after initialisation)
Based on what #musefan posted, here is an example. I had to move everything out of the constructor completely.
public class ServiceMain : System.ServiceProcess.ServiceBase
{
public const string SERVICE_NAME = "ServiceName";
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public ServiceMain()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
this.ServiceName = SERVICE_NAME;
this.CanPauseAndContinue = true;
}
static void Main()
{
//Log all unhandled exceptions
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new ServiceMain() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
components.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
//Do your stuff here
}
static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
{
Environment.Exit(1);
}
}