安装Windows服务使用C#会出错

时间:2013-02-06 15:42:40

标签: c# windows-services

我正在使用windows服务syncmysqldb.exe我需要服务来运行一个进程,即在setting.xml文件中给出的n个时间间隔内同步两个mysql数据库

当我安装此服务服务管理器时,我看到服务未处于启动状态,当我尝试通过右键单击手动运行时出现以下错误

当我安装服务时,它会出现以下错误 无法找到Source(MsiInstaller)中的事件ID(11001)的描述。本地计算机可能没有必要的注册表信息或消息DLL文件来显示来自远程计算机的消息。您可以使用/ AUXSOURCE =标志来检索此描述;请参阅帮助和支持以获取详细信以下信息是事件的一部分:产品:SetupSynMysqldb - 错误1001.错误1001.安装的提交阶段发生异常。此异常将被忽略,安装将继续。但是,安装完成后,应用程序可能无法正常运行。 - > savedState字典包含不一致的数据,可能已损坏。,(NULL),(NULL),(NULL),(NULL),,.

当我在服务管理器中运行服务时,它会出现以下错误 由于以下错误,SyncMysqlDb服务无法启动: 该系统找不到指定的文件。

C#代码

 protected override void OnStart(string[] args)
            {
                try
                {
                    //add this line to text file during start of service

                    EventLog.WriteEntry("SyncMysqlDb in OnStart. at " + DateTime.Now);
                    //handle Elapsed event
                    tmrSync.Elapsed += new ElapsedEventHandler(OnElapsedTime);



                    tmrSync.Interval = GetIntervals();

                    //enabling the timer
                    tmrSync.Enabled = true;
                    //TraceService(" tmrSync.Interval =" + tmrSync.Interval + " at " + DateTime.Now);
                    ThreadPool.QueueUserWorkItem(new WaitCallback(ServiceWorkerThread));
                }
                catch (Exception ex)
                {

                    EventLog.WriteEntry("Service failed to start.", EventLogEntryType.Error, (int)Service1EventIds.Start_InitializationFailure, ex);
                }
            }
private void ServiceWorkerThread(object state)
        {
            // Periodically check if the service is stopping.
            while (!this.stopping)
            {
                // Perform main service function here...

                Thread.Sleep(2000);  // Simulate some lengthy operations.
            }

            // Signal the stopped event.
            this.stoppedEvent.Set();
        }
        protected override void OnStop()
        {
            tmrSync.Enabled = false;
            //TraceService("stopping service" + DateTime.Now);
            EventLog.WriteEntry("SyncMysqlDb in OnStop. at " + DateTime.Now);
            this.stopping = true;
            this.stoppedEvent.WaitOne();
        }
        private void OnElapsedTime(object source, ElapsedEventArgs e)
        {

            try
            {
                EventLog.WriteEntry(@"Executing C:\SyncMysqlDbRepository\task.bat at " + DateTime.Now);
                Process.Start(@"C:\SyncMysqlDbRepository\task.bat");
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("Service failed to start.at " + DateTime.Now, EventLogEntryType.Error, (int)Service1EventIds.Start_InitializationFailure, ex);

            }
           // TraceService("syncing db at " + DateTime.Now);
        }
        protected int GetIntervals()
        {
            var dt = new DataTable();
            try
            {

                dt.ReadXmlSchema(@"C:\SyncMysqlDbRepository\SettingsDs.xml");
                dt.ReadXml(@"C:\SyncMysqlDbRepository\Settings.xml");

            }
            catch (Exception ex)
            {

                 EventLog.WriteEntry("GetIntervals  failed at " + DateTime.Now, EventLogEntryType.Error, (int)Service1EventIds.Start_InitializationFailure, ex);
                return 0;
            }
            return Convert.ToInt16(dt.Rows[0].ItemArray[2]); //Intervals from settings.xml
        }

0 个答案:

没有答案