none
C# how create scedule using windows service with time specification RRS feed

  • Question

  • hii, i have a problem to my project i want start automatic my program code using console app but dosen"t work, and now i try write code using windows service but how i can start ny code using windows service ??
    Friday, April 13, 2018 2:25 AM

Answers

  • Hi Ombi Lomri,

    Thank you for posting here.

    I am confused about what you want. What do you mean start automatic program code using console code?

    How do you want to write code using windows service? 

    If you want to the windows service do something at a special time every, you could try to use Timer.

    Here is a simple example in SO for your reference.

    https://stackoverflow.com/questions/503564/how-might-i-schedule-a-c-sharp-windows-service-to-perform-a-task-daily

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Ombi Lomri Monday, April 30, 2018 4:08 AM
    Friday, April 13, 2018 6:50 AM
    Moderator
  • For the basics see my code sample.  But be forewarned there is a good deal involved in writing and testing along with knowing what is not available to a service permission wise. 

    Also the service can be installed or uninstalled in program class

    using System;
    using System.Configuration.Install;
    using System.Reflection;
    using System.ServiceProcess;
    
    namespace NotificationEngine
    {
        internal static class Program
        {
            private static void Main(string[] args)
            {
                if (Environment.UserInteractive)
                {
                    string parameter = string.Concat(args);
                    switch (parameter)
                    {
                        case "--install":
                            ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
                            break;
    
                        case "--uninstall":
                            ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
                            break;
                    }
                }
                else
                {
                    ServiceBase[] ServicesToRun;
                    ServicesToRun = new ServiceBase[]
                    {
                        new NotificationService()
                    };
    
                    ServiceBase.Run(ServicesToRun);
                }
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Friday, April 13, 2018 7:35 PM
    Moderator

All replies

  • Hi Ombi Lomri,

    Thank you for posting here.

    I am confused about what you want. What do you mean start automatic program code using console code?

    How do you want to write code using windows service? 

    If you want to the windows service do something at a special time every, you could try to use Timer.

    Here is a simple example in SO for your reference.

    https://stackoverflow.com/questions/503564/how-might-i-schedule-a-c-sharp-windows-service-to-perform-a-task-daily

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Ombi Lomri Monday, April 30, 2018 4:08 AM
    Friday, April 13, 2018 6:50 AM
    Moderator
  • If you want your app to automatically start when a user logs in then put a shortcut in the Startup group in Windows. Alternatively use Task Scheduler. If you want to run an app on a schedule then use Task Scheduler.

    You only need to use a Windows service if you need to have code that runs continuously in the background and Task Scheduler is insufficient. Writing a service is a lot harder than writing a normal app and shouldn't be done unless you have a firm understanding of C# and how Windows works. Services are rarely the correct answer these days.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, April 13, 2018 2:31 PM
    Moderator
  • Are you using the Task Scheduler to schedule your program to run? If so then what is the problem you encounter? If not then can you do that?


    Sam Hobbs
    SimpleSamples.Info

    Friday, April 13, 2018 5:09 PM
  • For the basics see my code sample.  But be forewarned there is a good deal involved in writing and testing along with knowing what is not available to a service permission wise. 

    Also the service can be installed or uninstalled in program class

    using System;
    using System.Configuration.Install;
    using System.Reflection;
    using System.ServiceProcess;
    
    namespace NotificationEngine
    {
        internal static class Program
        {
            private static void Main(string[] args)
            {
                if (Environment.UserInteractive)
                {
                    string parameter = string.Concat(args);
                    switch (parameter)
                    {
                        case "--install":
                            ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
                            break;
    
                        case "--uninstall":
                            ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
                            break;
                    }
                }
                else
                {
                    ServiceBase[] ServicesToRun;
                    ServicesToRun = new ServiceBase[]
                    {
                        new NotificationService()
                    };
    
                    ServiceBase.Run(ServicesToRun);
                }
            }
        }
    }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Friday, April 13, 2018 7:35 PM
    Moderator
  • Your question is not clear. It is my understanding that you are trying to execute an application automatically on a pre-determined schedule and you tried to start the program automatically from a console program but that did not work so you are trying to write a service to automatically execute the application in a schedule.

    If that is not what you are trying to do then I don't understand how "start automatic my program code using console app" is related to "write code using windows service" because you say that you are writing the service because you could not do something using a console program. Is the question about starting and stopping a service or is the question about getting a program (not a service) executed automatically?



    Sam Hobbs
    SimpleSamples.Info


    Friday, April 13, 2018 7:55 PM