Esempio n. 1
0
        private void MainThread()
        {
            NextPort = 0;
            Threads  = new Dictionary <Int32, Thread>();
            eventLog1.BeginInit();
            STKTVMain.TVMain TvMain;
            DataRow          dr;
            bool             bLogged = false;

            TvMain = new STKTVMain.TVMain();

            do
            {
                if (pStopServiceEvent.WaitOne(1, false))
                {
                    try
                    {
                        //TvMain.EndInit();
                        //pRefService.InfoReport("Exiting working thread...");
                        InfoReport("Exiting working thread...");
                        pStopApprovedEvent.Set();
                        return;
                    }
                    catch (Exception Ex)
                    {
                        ErrorReport("Exiting working thread error: " + Ex.Message);

                        return;
                    }
                }
                try
                {
                    //pRefService.InfoReport("Try DB login.");
                    InfoReport("Try DB login.");
                    if (TvMain.Init(true) == true)
                    {
                        bLogged = true;
                    }
                    else
                    {
                        //pRefService.WarningReport("Unable to login, check credentials");
                        WarningReport("Unable to login, check credentials");
                        InfoReport(TvMain.GetEnvInfo());
                    }
                }
                catch (Exception Ex)
                {
                    //pRefService.ErrorReport("Login failed, try again... " + Ex.Message);
                    ErrorReport("Login failed, try again... " + Ex.Message);
                    InfoReport(TvMain.GetEnvInfo());
                }
            } while (!pStopServiceEvent.WaitOne(1000, false) && !bLogged);
            //pRefService.InfoReport("DB Initialization OK");
            InfoReport("DB Initialization OK");
            //bool isgetplantable = false;

            //ƒл¤ проверки работы сервиса
            //Int16 a = 5;
            //while (a < 6)
            //{
            //    a = 5;
            //}

            do
            {
                System.Data.DataTable oRS;
                ThreadObj             throbj = null;

                oRS = null;

                if (DivID == 0)
                {
                    oRS = TvMain.GetDBDevicePlanList();
                }
                else
                {
                    oRS = TvMain.GetDBOneDevicePlanList(DivID);
                }

                if (oRS != null)
                {
                    //isgetplantable = true;
                    NextPort = 0;
                    if (oRS.Rows.Count > 0)
                    {
                        InfoReport("Checking " + oRS.Rows.Count + " active plan(s) at " + DateTime.Now.ToString());
                        for (int i = 0; i < oRS.Rows.Count; i++)
                        {
                            try
                            {
                                dr    = oRS.Rows[i];
                                id_bd = Convert.ToInt32(dr["id_bd"].ToString());
                                //if (Threads[id_bd] == null)
                                if (!Threads.ContainsKey(id_bd))
                                {
                                    throbj = new ThreadObj();
                                    //throbj.dr = oRS.Rows[i];
                                    throbj.dr = null;
                                    throbj.dr = dr;

                                    //throbj.TvMain = TvMain;
                                    throbj.TvMain = new STKTVMain.TVMain();
                                    throbj.TvMain.Init(false);
                                    //throbj.TvMain.connect();

                                    Threads.Add(id_bd, new Thread(new ParameterizedThreadStart(this.CallThr)));
                                    Threads[id_bd].SetApartmentState(ApartmentState.MTA);
                                    Threads[id_bd].Name = "Service thread " + id_bd.ToString();
                                    //InfoReport("Starting  thread " + id_bd.ToString());
                                    Threads[id_bd].Start(throbj);
                                }
                                else
                                {
                                    if (Threads[id_bd].IsAlive == false)
                                    {
                                        throbj = new ThreadObj();
                                        //throbj.dr = oRS.Rows[i];
                                        throbj.dr = null;
                                        throbj.dr = dr;
                                        //throbj.TvMain = TvMain;
                                        throbj.TvMain = new STKTVMain.TVMain();
                                        throbj.TvMain.Init(false);
                                        Threads[id_bd] = new Thread(new ParameterizedThreadStart(this.CallThr));
                                        Threads[id_bd].SetApartmentState(ApartmentState.MTA);
                                        Threads[id_bd].Name = "Service thread " + id_bd.ToString();
                                        //InfoReport("Restarting  thread " + id_bd.ToString());
                                        Threads[id_bd].Start(throbj);
                                    }
                                }
                            }
                            catch (Exception Ex)
                            {
                                ErrorReport("Thread " + id_bd.ToString() + " error:" + Ex.Message);
                            }
                        }
                    }
                    oRS    = null;
                    throbj = null;
                }
            } while (!pStopServiceEvent.WaitOne(TvMain.RequestInterval, false));

            try
            {
                InfoReport("Closing STKService...");
                Threads.Clear();
                Threads = null;
                dr      = null;

                // close nport library
                TvMain.EndInit(true);
                TvMain.CloseDBConnection();
                TvMain = null;
                //eventLog1.Close();
                eventLog1.Dispose();
                return;
            }
            catch (Exception Ex)
            {
                ErrorReport("Closing STKService error:" + Ex.Message);
            }
        }