Skip to main content

To invoke a specified application at specified time

This is the windows service which will invoke a application at 5.30 pm


Here I am invoking a web page

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Timers;
using System.Configuration;
using System.Globalization;


namespace WinWin
{

public class WinService1 : System.ServiceProcess.ServiceBase
{

///

/// Required designer variable.
///

///
// private System.Timers.Timer aTimer;
private System.ComponentModel.Container components = null;
//private System.Timers.Timer aTimer = new System.Timers.Timer();
//private System.ComponentModel.Container components = null;

private System.Timers.Timer aTimer;
public WinService1()
{
//System.Timers.Timer aTimer;
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();

// TODO: Add any initialization after the InitComponent call
}

// The main entry point for the process
//private System.Timers.Timer aTimer;
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;

// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//

ServicesToRun = new System.ServiceProcess.ServiceBase[] { new WinService1() };

System.ServiceProcess.ServiceBase.Run(ServicesToRun);




}

///

/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
//
// WinService1
//
this.ServiceName = "WinService1";

}

///

/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

///

/// Set things in motion so your service can do its work.
///

///


private Timer timer;
protected override void OnStart(string[] args)
{


if (aTimer == null)
{
aTimer = new System.Timers.Timer();
aTimer.Elapsed +=
new ElapsedEventHandler(OnTimer);
//Let the timer trigger once a minute
aTimer.Interval = 59000;
aTimer.Enabled = true;
}
else
{
aTimer.Start();
}




/*


// TODO: Add code here to start your service.
DateTime currentSystemTime = DateTime.Now;

//System.Timers.Timer aTimer = new System.Timers.Timer();

// Hook up the Elapsed event for the timer.
//aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

// Set the Interval to 2 seconds (2000 milliseconds).
//aTimer.Interval = 2000;
//aTimer.Enabled = true;

// TODO: Add code here to start your service.
int i = 1;
if(i == 1)
{
if(currentSystemTime.Hour == 18 & currentSystemTime.Minute == 0)
{
//Console.WriteLine ("In the Main");
//Console.WriteLine ("In the Main");
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.FileName="iexplore";
proc.StartInfo.Arguments="url";
proc.Start();
//proc.WaitForExit();

}
else
{
}
}


*/

}


private void InitializeTimer()
{
if (timer == null)
{
timer = new Timer();
timer.AutoReset = true;
timer.Interval = 60000 * Convert.ToDouble(
ConfigurationSettings.AppSettings["IntervalMinutes"]);
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
}
}

private void timer_Elapsed(object source,System.Timers.ElapsedEventArgs e)
{
RunCommands();
}


private void RunCommands()
{
// JobRunWebService.JobRunInterval objJob =
// new JobRunWebService.JobRunInterval();
// objJob.RunJob();
}

///

/// Stop this service.
///

protected override void OnStop()
{
if (aTimer != null) aTimer.Stop();
// TODO: Add code here to perform any tear-down necessary to stop your service.
}

public static void OnTimer(Object source,ElapsedEventArgs e)
{
RunDataLoader();
}
private static void RunDataLoader()
{
//Do your job in this procedure...
//while()


DateTime currentSystemTime = DateTime.Now;

//currentSystemTime.s
if(currentSystemTime.Hour == 17 & currentSystemTime.Minute == 30)
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.EnableRaisingEvents=false;
proc.StartInfo.FileName="iexplore";
proc.StartInfo.Arguments="url";
proc.Start();
}
}


protected override void OnPause()
{
if (aTimer != null) aTimer.Stop();
}
protected override void OnContinue()
{
if (aTimer != null) aTimer.Start();
}


}

}

Comments

Popular posts from this blog

Task Parallel Library (TPL) and Akka.NET: Differences

Task Parallel Library (TPL) and Akka.NET are both powerful tools in the .NET ecosystem for handling parallelism and concurrency, but they serve different purposes and use different models of computation. Here are some key differences:s 1.    Actor Model vs Task-Based Model: Akka.NET is built around the actor model, where actors are the fundamental units of computation and they communicate by exchanging messages. TPL, on the other hand, is task-based. It's designed to make developers more productive by simplifying the process of adding parallelism and concurrency to applications. TPL uses tasks (which are independently executing work units) and provides various ways to control and coordinate them. 2.    Fault Tolerance: One of the key features of Akka.NET is its built-in fault tolerance. It has a "let-it-crash" philosophy, where the system is designed to self-heal from errors. If an actor fails, its parent actor can decide on the supervision strategy: either to resta

Extension Methods - Advanced

Here we will see how can we use the Extension Methods in advanced manner in other types Imagine you often need to retrieve items from a List based on a custom criterion that the built-in LINQ methods don't cover. Extension Methods for Lists: Filtering based on Custom Criteria And the output would be   Extending Enums: Displaying Descriptive Strings Output: Extending DateTime: Calculating Age     Output: The code samples can be found at https://github.com/oneananda/C_Sharp_Examples/tree/main/ExtensionMethods