繁体   English   中英

Windows Service卡在“启动”上

[英]Windows Service Stuck on “Starting”

我刚刚创建并安装了我的第一个Windows服务。 当我启动该服务时,它永远不会更改为“已启动”状态。 状态保持为“正在启动”,但服务正在执行它的工作。 我以为也许是我与OnStart方法进行交互的方式。 我只是简单地使用OnStart方法来调用另一个执行良好的方法。 这是一个示例:

        protected override void OnStart(string[] args)
    {
        try { 
                Logger("Start");
            }
        catch (Exception ex)
        {
            string filePath2 = @"C:/ProgramData/Error.txt";

            using (StreamWriter writer = new StreamWriter(filePath2, true))
            {
                writer.WriteLine(DateTime.Now + Environment.NewLine + "Message: " + ex.ToString() + Environment.NewLine + "Stack Trace: " + ex.StackTrace);
            }
        }
    }

我需要更改什么以使客户端注册该服务已启动并正在运行。 PS,该服务正在执行其应有的功能。

在此先感谢您提供的所有帮助!

编辑

这是Logger的作用:

public void Logger(string state)
    {
        try
        {
            {
                Random a = new Random(Environment.TickCount);
                //unique name PhoneSystem.ApplicationName = "TestApi";//any name
                PhoneSystem.ApplicationName = PhoneSystem.ApplicationName + a.Next().ToString();
            }

            #region phone system initialization(init db server)
            String filePath = @"C:/ProgramData/3CXLogger/3CXPhoneSystem.ini";
            if (!File.Exists(filePath))
            {
                //this code expects 3CXPhoneSystem.ini in current directory.
                //it can be taken from the installation folder (find it in Program Files/3CXPhone System/instance1/bin for in premiss installation)
                //or this application can be run with current directory set to location of 3CXPhoneSystem.ini

                //v14 (cloud and in premiss) installation has changed folder structure.
                //3CXPhoneSystem.ini which contains connectio information is located in 
                //<Program Files>/3CX Phone System/instanceN/Bin folder.
                //in premiss instance files are located in <Program Files>/3CX Phone System/instance1/Bin
                throw new Exception("Cannot find 3CXPhoneSystem.ini");
            }
            String value = _3cxLogger.Utilities.GetKeyValue("ConfService", "ConfPort", filePath);
            Int32 port = 0;
            if (!String.IsNullOrEmpty(value))
            {
                Int32.TryParse(value.Trim(), out port);
                PhoneSystem.CfgServerPort = port;
            }
            value = _3cxLogger.Utilities.GetKeyValue("ConfService", "confUser", filePath);
            if (!String.IsNullOrEmpty(value))
                PhoneSystem.CfgServerUser = value;
            value = _3cxLogger.Utilities.GetKeyValue("ConfService", "confPass", filePath);
            if (!String.IsNullOrEmpty(value))
                PhoneSystem.CfgServerPassword = value;
            #endregion
            DN[] ps = PhoneSystem.Root.GetDN(); //Access PhoneSystem.Root to initialize ObjectModel
            //_3cxLogger.SampleStarter.StartSample(args);
        }
        catch (Exception ex)
        {
            string filePath2 = @"C:\ProgramData\3CXLogger\Error.txt";

            using (StreamWriter writer = new StreamWriter(filePath2, true))
            {
                writer.WriteLine(DateTime.Now + Environment.NewLine + "Message: " + ex.ToString() + Environment.NewLine + "Stack Trace: " + ex.StackTrace);
            }
            //Console.WriteLine(ex.ToString());
        }
        string constring = "Data Source = LEWCOMP1\\COMPLIANCE; Initial Catalog = 3CXCallStats; Integrated Security = True";


        while (state == "Start")
        {
            Thread.Sleep(5000);
            int count = 0;
            foreach (DN dn in PhoneSystem.Root.GetDN())
            {
                ActiveConnection[] a = dn.GetActiveConnections();
                foreach (ActiveConnection ac in a)
                {
                    try
                    {
                        if (ac.Status == ConnectionStatus.Connected)
                        {
                            count = count + 1;
                        }
                    }
                    catch (Exception ex)
                    {
                        //Console.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.Source);
                        string filePath2 = @"C:\ProgramData\3CXLogger\Error.txt";

                        using (StreamWriter writer = new StreamWriter(filePath2, true))
                        {
                            writer.WriteLine(DateTime.Now + Environment.NewLine + "Message: " + ex.ToString() + Environment.NewLine + "Stack Trace: " + ex.StackTrace);
                        }
                    }
                }
            }
            count = count / 2;
            string update = "UPDATE callsCounter SET Counter = '" + count + "' WHERE ID='1';";
            string insert = "INSERT Interval_Counter (Date_Time, Count) VALUES ('" + DateTime.Now + "','" + count + "')";
            SqlConnection myCon = new SqlConnection(constring);
            SqlCommand updateCMD = new SqlCommand(update, myCon);
            SqlCommand insertCMD = new SqlCommand(insert, myCon);
            SqlDataReader myReaderUpdate;
            SqlDataReader myReaderInsert;

            myCon.Open();
            myReaderUpdate = updateCMD.ExecuteReader();
            myReaderUpdate.Read();
            myCon.Close();


            myCon.Open();
            myReaderInsert = insertCMD.ExecuteReader();
            myReaderInsert.Read();
            myCon.Close();
        }
    }

另外,我检查了事件日志,并发现该服务已成功启动的事件。 奇。

感谢您的所有帮助!

我创建了一个新类并启动了一个针对该方法的新线程。

protected override void OnStart(string[] args)
    {
        Log oLog = new Log();
        Thread t = new Thread(new ThreadStart(oLog.Logger));
        t.Start();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM