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

Using of global variables in C# - Drawbacks & Solutions

How using global variables can have implications on the design, maintainability, and test-ability of C# code: Harder to understand and reason about the code:       class Program     {         public static int globalCounter = 0;         static void Main()         {             globalCounter++;             Console.WriteLine(globalCounter);         }     }   In this example, the global variable globalCounter is accessible from anywhere in the program, including the Main method. It's not clear where the value of the globalCounter is updated, it could be updated in other methods or classes, making it harder to trace the flow of data and understand the source of bugs.   More prone to errors:       class Program     {         public static string globalString;         static void Main()         {             globalString = "Hello" ;             Method1();             Method2();         }         static void Method1()         {

Task Parallel Library (TPL) and Akka.NET Alternatives

Task Parallel Library (TPL) and Akka.NET are among the most commonly used libraries for parallel and concurrent programming in the .NET ecosystem. However, there are also several other options available, depending on your specific needs: Parallel Language Integrated Query (PLINQ) is a parallel programming feature of .NET that provides an easy and efficient way to perform operations on collections in parallel. LINQ (Language Integrated Query) is a powerful feature in .NET that allows developers to work with data in a more declarative and language-integrated manner. While LINQ queries are inherently sequential, PLINQ extends LINQ by providing parallel versions of the query operators, allowing some queries to execute faster by utilizing multiple processors or cores on a machine. PLINQ is great when you are working with large collections where operations might be CPU-intensive or I/O-bound and could potentially be sped up by parallel execution. Here is a simple example of a PLI

SOLID Principles with Real World examples in C#

  SOLID Principles with Real World examples in C#   SOLID principles are formed by using S Single Responsibility Principles (SRP) O Open Closed Principle (OCP) L Liskov’s Substitution Principle (LCP) I Interface Segregation Principle (ISP) D Dependency Inversion Principle (DIP)   S Single Responsibility Principles (SRP) There should never be more than one reason for a class to change, to be precise one class should have only one responsibility Single Responsibility Principles (SRP) Real world example, A perfect match for SRP is Microservices , a Microservice will not contain functionalities other than the one it is designated to do,  Example ·                   Order Processing Service, ·                   Shipment Management Service, ·                   User Authentication Service, ·                   Catalogue List Service       class OrderProcessor     {         public void Process(Order order)         {             // Check inven