locked
Windows Service in C#. Where do I place my code? RRS feed

  • Question

  • I've created a simple C# exe that performs some FTP stuff and its quite nice, but I want it to be a Windows service. I'm lost as to where to place my code or how to call it? Help!

    Here is the basic code for Service1.cs. Where does the infinite loop go? etc... My C# FTP program works within "static void Main(string[] args)". I'm lost.

    namespace WindowsService
    {
        public partial class Service1 : ServiceBase
        {
            public Service1()
            {
                InitializeComponent();
            }

            protected override void OnStart(string[] args)
            {               
            }

            protected override void OnStop()
            {
            }
        }
    }


    Please mark my post as helpful or the answer or better yet.... both! :) Thanks!

    Thursday, October 24, 2013 9:09 PM

Answers

  • In OnStart create a thread to run the code in your Main().

    David


    David http://blogs.msdn.com/b/dbrowne/

    • Proposed as answer by RohitArora Friday, October 25, 2013 4:54 AM
    • Marked as answer by zperryz Saturday, October 26, 2013 3:05 PM
    Thursday, October 24, 2013 9:59 PM

All replies

  • In OnStart create a thread to run the code in your Main().

    David


    David http://blogs.msdn.com/b/dbrowne/

    • Proposed as answer by RohitArora Friday, October 25, 2013 4:54 AM
    • Marked as answer by zperryz Saturday, October 26, 2013 3:05 PM
    Thursday, October 24, 2013 9:59 PM
  • Here is the problem..... I have an infinite loop in my program, so it runs until it is stopped. When I place the loop in the OnStart(), I'm unable to start the service after install. Even if I call the Main() in the Onstart. Any other suggestions?

    Please mark my post as helpful or the answer or better yet.... both! :) Thanks!


    Update..... I tried it without a loop and it works in Onstart, but looping is required. Where do I place it.
    • Edited by zperryz Saturday, October 26, 2013 2:20 PM More info
    Saturday, October 26, 2013 1:59 PM
  • I didn't know how to create a thread. After some research and trial and error I figured it out and you were right. A loop is fine it its called via a thread in onstart. Yay! Here is what made it work for others trying to figure this out.

    protected override void OnStart(string[] args)
            {
                var maincode = new Thread(YourCode);
                maincode.Name = "Your Code";
                maincode.IsBackground = false;
                maincode.Start();
            }
    void YourCode()
            {
                while (true)
                {  
                    string path = @"c:\temp\sucess.txt";
                    using (StreamWriter txt = File.AppendText(path))
                    {
                        txt.WriteLine("This is working");
                    }
                    Thread.Sleep(5000);
                }
            }


    Please mark my post as helpful or the answer or better yet.... both! :) Thanks!

    Saturday, October 26, 2013 3:05 PM