コード例 #1
0
ファイル: RService.cs プロジェクト: windygu/TEPLOMER
 private void StopDeviceThreads()
 {
     if (srv != null)
     {
         srv.StopServer();
     }
     srv = null;
 }
コード例 #2
0
ファイル: RService.cs プロジェクト: windygu/TEPLOMER
        private void MainThread()
        {
            InfoReport("Load config from :" + GetMyDir() + "\\ATMServiceCFG.xml");
            int         RequestInterval = 60000;
            XmlDocument xml             = null;
            XmlElement  node            = null;

            xml = new XmlDocument();
            try
            {
                xml.Load(GetMyDir() + "\\ATMServiceCFG.xml");

                node = (XmlElement)xml.LastChild;
            }
            catch (Exception)
            {
            }

            String sAddr;
            short  nPort;

            try{
                sAddr = node.Attributes.GetNamedItem("serveraddress").Value;
            }catch (System.Exception ex) {
                sAddr = "192.168.9.146";
            }

            try{
                nPort = Convert.ToInt16(node.Attributes.GetNamedItem("serverport").Value);
            }catch (System.Exception ex) {
                nPort = 16899;
            }

            eventLog1.BeginInit();



            bool bLogged = false;

            do
            {
                bLogged = false;
                do
                {
                    // случилось команда на выход из сервиса
                    if (pStopServiceEvent.WaitOne(1, false))
                    {
                        try
                        {
                            InfoReport("Exiting working thread");
                            pStopApprovedEvent.Set();
                            return;
                        }
                        catch (Exception Ex)
                        {
                            ErrorReport("Exiting working thread error: " + Ex.Message);
                            return;
                        }
                    }



                    // запускаем TCP  сервер слушать порт
                    try
                    {
                        if (srv == null)
                        {
                            InfoReport("Start ATM Server");
                            srv = new atmServer.TCPServer(sAddr, nPort);
                            srv.StartServer();
                        }
                        else
                        {
                            if (!srv.IsLive())
                            {
                                try
                                {
                                    InfoReport("Stop ATM Server");
                                    srv.StopServer();
                                    srv = null;
                                }
                                catch (Exception Ex)
                                {
                                    ErrorReport("Error while stopping TCP Listenner... " + Ex.Message);
                                }
                            }
                        }
                    }
                    catch (Exception Ex)
                    {
                        ErrorReport("Error while start TCP Listenner... " + Ex.Message);
                    }

                    System.Threading.Thread.Sleep(1000);
                } while (!pStopServiceEvent.WaitOne(1000, false) && !bLogged);
            } while (!pStopServiceEvent.WaitOne(RequestInterval, false));

            try
            {
                InfoReport("Closing ATMService...");
                StopDeviceThreads();
                base.OnStop();
                eventLog1.Dispose();
                return;
            }
            catch (Exception Ex)
            {
                ErrorReport("Closing ATMService error:" + Ex.Message);
            }
        }