开发者

Passing parameter to a windows service in c#

开发者 https://www.devze.com 2023-01-11 02:27 出处:网络
I am creating a windows service which has to run on specific days and time. I am passing these variables using an XML document. Here is my code for passing the values

I am creating a windows service which has to run on specific days and time. I am passing these variables using an XML document. Here is my code for passing the values

   static void Main开发者_运维知识库()
    {
        ServiceBase[] ServicesToRun;

        Service1= new Service1();          

        //Load setting from xml and assign to variables "daysToExec" and "timeToExec"
        LoadSettings();
        Service1.daysToExecute = daysToExec;
        Service1.timeToExecute = timeToExec;
        ServicesToRun = new ServiceBase[] { Service1 };
        ServiceBase.Run(ServicesToRun);          

    }

The code for OnStart() in Service1.cs

    protected override void OnStart(string[] args)
    {
     timer1.Enable = true;
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        foreach (string DayToRun in daysToExecute)
        {
            if (DateTime.Now.DayOfWeek.ToString().ToUpper().Equals(DayToRun.ToUpper())  && DateTime.Now.ToShortTimeString().Equals(timeToExecute)) 
            {
                Process.Start("Path to executable");
            }
        }
    }

But this is not starting the executable. Is there something wrong with this code.

Thanks.


I got the answer atlast. I was using System.Windows.Forms.Timer and for some reason it was not at all executing the timer_tick method. After using System.Timers.Timer it worked fine.

Thanks for your comments.

0

精彩评论

暂无评论...
验证码 换一张
取 消