Ejemplo n.º 1
0
        /// <summary>
        /// Обновить словарь из файла, если файл изменился
        /// </summary>
        public bool Update(out bool changed, out string errMsg)
        {
            DateTime newFileAge = ScadaUtils.GetLastWriteTime(fileName);

            if (newFileAge > DateTime.MinValue && FileAge == newFileAge)
            {
                changed = false;
                errMsg  = "";
                return(true);
            }
            else if (Localization.LoadDictionaries(fileName, out errMsg))
            {
                initPhrasesAction?.Invoke();
                initialUpdate = false;
                FileAge       = newFileAge;
                changed       = true;
                return(true);
            }
            else
            {
                // вывод ошибки в журнал
                log?.WriteError(errMsg);

                // загрузка словаря по умолчанию
                if (initialUpdate)
                {
                    initialUpdate = false;
                    Localization.LoadDictionaries(defaultFileName, out string errMsg2);
                    initPhrasesAction?.Invoke();
                }

                changed = false;
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Обновить настройки из файла, если файл изменился
        /// </summary>
        public bool Update(out bool changed, out string errMsg)
        {
            DateTime newFileAge = ScadaUtils.GetLastWriteTime(fileName);

            if (newFileAge > DateTime.MinValue && FileAge == newFileAge)
            {
                changed = false;
                errMsg  = "";
                return(true);
            }
            else
            {
                ISettings settings = recreate ? Settings.Create() : Settings;

                if (settings.LoadFromFile(fileName, out errMsg))
                {
                    FileAge = newFileAge;

                    if (recreate)
                    {
                        changed  = !Settings.Equals(settings);
                        Settings = settings;
                    }
                    else
                    {
                        changed = true;
                    }

                    return(true);
                }
                else
                {
                    if (log != null)
                    {
                        log.WriteError(errMsg);
                    }

                    changed = false;
                    return(false);
                }
            }
        }
Ejemplo n.º 3
0
        public static void ReloadItems(this ListBox listBox, string fileName, bool fullLoad,
                                       ref DateTime fileAge)
        {
            Monitor.Enter(listBox);

            try
            {
                if (File.Exists(fileName))
                {
                    DateTime newFileAge = ScadaUtils.GetLastWriteTime(fileName);

                    if (fileAge != newFileAge)
                    {
                        // загрузка строк из файла
                        List <string> stringList = LoadStrings(fileName, fullLoad);
                        int           newLineCnt = stringList.Count;

                        // проверка для исключения отображения данных, считыванных в момент записи файла
                        if (newLineCnt > 0 || (DateTime.Now - newFileAge).TotalMilliseconds > 50)
                        {
                            fileAge = newFileAge;

                            // вывод данных в список
                            int oldLineCnt    = listBox.Items.Count;
                            int selectedIndex = listBox.SelectedIndex;
                            int topIndex      = listBox.TopIndex;

                            listBox.BeginUpdate();

                            for (int i = 0; i < newLineCnt; i++)
                            {
                                if (i < oldLineCnt)
                                {
                                    listBox.Items[i] = stringList[i];
                                }
                                else
                                {
                                    listBox.Items.Add(stringList[i]);
                                }
                            }

                            for (int i = newLineCnt; i < oldLineCnt; i++)
                            {
                                listBox.Items.RemoveAt(newLineCnt);
                            }

                            // установка позиции прокрутки списка
                            if (listBox.SelectionMode == SelectionMode.One && newLineCnt > 0)
                            {
                                if (selectedIndex < 0 && !fullLoad)
                                {
                                    listBox.SelectedIndex = newLineCnt - 1; // прокрутка в конец списка
                                }
                                else
                                {
                                    listBox.TopIndex = topIndex;
                                }
                            }

                            listBox.EndUpdate();
                        }
                    }
                }
                else
                {
                    if (listBox.Items.Count == 1)
                    {
                        listBox.Items[0] = CommonPhrases.NoData;
                    }
                    else
                    {
                        listBox.Items.Clear();
                        listBox.Items.Add(CommonPhrases.NoData);
                    }
                    fileAge = DateTime.MinValue;
                }
            }
            catch (Exception ex)
            {
                if (listBox.Items.Count == 2)
                {
                    listBox.Items[0] = CommonPhrases.ErrorWithColon;
                    listBox.Items[1] = ex.Message;
                }
                else
                {
                    listBox.Items.Clear();
                    listBox.Items.Add(CommonPhrases.ErrorWithColon);
                    listBox.Items.Add(ex.Message);
                }
                fileAge = DateTime.MinValue;
            }
            finally
            {
                Monitor.Exit(listBox);
            }
        }
Ejemplo n.º 4
0
        public static void ReloadItems(this ListBox listBox, string fileName, bool fullLoad,
                                       ref DateTime fileAge)
        {
            Monitor.Enter(listBox);

            try {
                if (File.Exists(fileName))
                {
                    var newFileAge = ScadaUtils.GetLastWriteTime(fileName);

                    if (fileAge == newFileAge)
                    {
                        return;
                    }

                    // loading lines from file
                    List <string> stringList = LoadStrings(fileName, fullLoad);
                    int           newLineCnt = stringList.Count;

                    // check to exclude the display of data read at the time of writing the file
                    if (newLineCnt <= 0 && !((DateTime.Now - newFileAge).TotalMilliseconds > 50))
                    {
                        return;
                    }

                    fileAge = newFileAge;

                    // list output
                    int oldLineCnt    = listBox.Items.Count;
                    int selectedIndex = listBox.SelectedIndex;
                    int topIndex      = listBox.TopIndex;

                    listBox.BeginUpdate();

                    for (var i = 0; i < newLineCnt; i++)
                    {
                        if (i < oldLineCnt)
                        {
                            listBox.Items[i] = stringList[i];
                        }
                        else
                        {
                            listBox.Items.Add(stringList[i]);
                        }
                    }

                    for (int i = newLineCnt; i < oldLineCnt; i++)
                    {
                        listBox.Items.RemoveAt(newLineCnt);
                    }

                    // setting the scroll position
                    if (listBox.SelectionMode == SelectionMode.One && newLineCnt > 0)
                    {
                        if (selectedIndex < 0 && !fullLoad)
                        {
                            listBox.SelectedIndex = newLineCnt - 1; // scroll to end of list
                        }
                        else
                        {
                            listBox.TopIndex = topIndex;
                        }
                    }

                    listBox.EndUpdate();
                }
                else
                {
                    if (listBox.Items.Count == 1)
                    {
                        listBox.Items[0] = CommonPhrases.NoData;
                    }
                    else
                    {
                        listBox.Items.Clear();
                        listBox.Items.Add(CommonPhrases.NoData);
                    }

                    fileAge = DateTime.MinValue;
                }
            } catch (Exception ex) {
                if (listBox.Items.Count == 2)
                {
                    listBox.Items[0] = CommonPhrases.ErrorWithColon;
                    listBox.Items[1] = ex.Message;
                }
                else
                {
                    listBox.Items.Clear();
                    listBox.Items.Add(CommonPhrases.ErrorWithColon);
                    listBox.Items.Add(ex.Message);
                }

                fileAge = DateTime.MinValue;
            } finally {
                Monitor.Exit(listBox);
            }
        }