Looking for help in understanding C# windows services I don't understand microsofts poorly written example

  • Thread starter Thread starter PotholesInMyLawn
  • Start date Start date
P

PotholesInMyLawn

Guest
Hi all... thanks in advance for any help...


I am trying to following this slop... its a very poor example... I will never understand why these examples are not realworld examples that make sense... any way... my issues...

I created a console application that I am not trying to make into a windows service.. I moved all the stuff over to the service and it compiles....


I did the service install but I am guessing the service is not doing what I expect because the instructions are so poorly written as usual when it comes to these type of examples..


Where do you put the functions that you want to run lets say every minute?

Does that stuff go in the OnStart or does it go In the OnrTimer Where I currently have it.

I will admit I don't understand how this all work as it is my first windows service and the direction on how to make this so are a horror show! Thanks


This is what I have:

namespace AWS808
{
public partial class ASSA_SERVICE : ServiceBase
{
private EventLog eventLog1;
RequestDC_Class RDC = new RequestDC_Class();
ASA_CloseService_Class ACS = new ASA_CloseService_Class();


[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool SetServiceStatus(System.IntPtr handle, ref ServiceStatus serviceStatus);


public ASSA_SERVICE()
{
InitializeComponent();

try
{
eventLog1 = new System.Diagnostics.EventLog();
if (!System.Diagnostics.EventLog.SourceExists("ASSASource"))
{
System.Diagnostics.EventLog.CreateEventSource("ASSASource", "ASSA_Run_Log");
}
eventLog1.Source = "ASSASource";
eventLog1.Log = "ASSA_Run_Log";
}

catch
{

}

}

protected override void OnStart(string[] args)
{
try
{
eventLog1.WriteEntry("ASSA OnStart");

}
catch
{

}

// Update the service state to Start Pending.
ServiceStatus serviceStatus = new ServiceStatus();
serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING;
serviceStatus.dwWaitHint = 100000;
SetServiceStatus(this.ServiceHandle, ref serviceStatus);

// Set up a timer that triggers every minute.
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 60000; // 60 seconds
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimer);
timer.Start();

// Update the service state to Running.
serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
SetServiceStatus(this.ServiceHandle, ref serviceStatus);

}

private void OnTimer(object sender, ElapsedEventArgs e)
{
//throw new NotImplementedException();


//Process ASA Close Request
ACS.ASA_CheckIn();

//Check 808 Status
RDC.ResetLifeCycle();


}

protected override void OnStop()
{
// Update the service state to Start Pending.
ServiceStatus serviceStatus = new ServiceStatus();
serviceStatus.dwCurrentState = ServiceState.SERVICE_START_PENDING;
serviceStatus.dwWaitHint = 100000;
SetServiceStatus(this.ServiceHandle, ref serviceStatus);


// Update the service state to Running.
serviceStatus.dwCurrentState = ServiceState.SERVICE_RUNNING;
SetServiceStatus(this.ServiceHandle, ref serviceStatus);

}


public enum ServiceState
{
SERVICE_STOPPED = 0x00000001,
SERVICE_START_PENDING = 0x00000002,
SERVICE_STOP_PENDING = 0x00000003,
SERVICE_RUNNING = 0x00000004,
SERVICE_CONTINUE_PENDING = 0x00000005,
SERVICE_PAUSE_PENDING = 0x00000006,
SERVICE_PAUSED = 0x00000007,
}

[StructLayout(LayoutKind.Sequential)]
public struct ServiceStatus
{
public int dwServiceType;
public ServiceState dwCurrentState;
public int dwControlsAccepted;
public int dwWin32ExitCode;
public int dwServiceSpecificExitCode;
public int dwCheckPoint;
public int dwWaitHint;
};

}
}

Continue reading...
 
Back
Top