msk32asu 0 Newbie Poster

Hi,

I have a Windows Socket Server console app which executes power shell scripts, and it is working great. But when I try to covert it into a Windows Service it doesnt work. The service is created it does not hang as such, but when I ping the IP/Port of the socket server this service is supposed to be hosting nothing comes up.

Here are some snippets of the code, any help would be greatly appreciated:

, [code=c#]
Main Program.cs:

        static void Main()
        {
       
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new PowerService()
            };
            ServiceBase.Run(ServicesToRun);
        }

Then Service1.cs:

    public partial class PowerService : ServiceBase
    {
        private bool isStopped = false;
        System.Threading.Thread t = null;
       

        public PowerService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            this.isStopped = false;
            PowerServer ps = new PowerServer();
            Thread t = new Thread(new System.Threading.ThreadStart(ps.PowerMethod));
            t.Start();

        }

        protected override void OnStop()
        {
            this.isStopped = true;
            t.Abort();
           
        }

And then ther server itself:

public void PowerMethod()       
        {
            client = new TcpListener(IPAddress.Any,8080);
            client.Start();
            while (true)
            {
                while (!client.Pending())
                {
                    Thread.Sleep(1000);
                }
                ConnectionThread newconnection = new ConnectionThread();
                newconnection.threadListener = this.client;
                Thread newthread = new Thread(new ThreadStart(newconnection.HandleConnection));
                newthread.Start();
            }

There are threads spawning more therads, but it was working as a console app, and is starting as a windows service - any advices?

Best Regards,
SK

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.