Example #1
0
        public Timer()
        {
            InitializeComponent();
            try
            {
                var t = Properties.Settings.Default["LeftOffset"].ToString();
                Left = int.Parse(Properties.Settings.Default["LeftOffset"].ToString());
                Top  = int.Parse(Properties.Settings.Default["TopOffset"].ToString());
            }
            catch (Exception e) { }

            settings[Settings.StartTime] = DateTime.Now;
            settings[Settings.RelaxTime] = new TimeSpan(0, int.Parse(GetValueFromConfig("relaxMinutes")), 0);
            settings[Settings.WorkTime]  = new TimeSpan(0, int.Parse(GetValueFromConfig("workMinutes")), 0);

            settings[Settings.CheckTime] = new TimeSpan(0, int.Parse(GetValueFromConfig("waitForCheckMinutes")), 0);
            WorkState = State.Work;

            AttentSelector.getInstance().Init();

            thread = new Thread(MainLogic);
            thread.Start();
        }
Example #2
0
        private void MainLogic()
        {
            checkedToggle = false;
            TimeSpan offsetTime            = DateTime.Now.TimeOfDay;
            TimeSpan startWaitToCheckStamp = DateTime.Now.TimeOfDay;
            bool     isContinueWait        = false;

            while (true)
            {
                if (isContinueWait)
                {
                    isContinueWait = false;
                }
                else
                {
                    lock (monitor)
                        Monitor.Wait(monitor, TimeSpan.FromSeconds(1));
                }

                switch (WorkState)
                {
                case State.Work:
                case State.Relax:     // 20 - 5 = 15
                    offsetTime = DateTime.Now - (DateTime)settings[Settings.StartTime];

                    if ((offsetTime > (TimeSpan)settings[Settings.RelaxTime] && WorkState == State.Relax) ||
                        (offsetTime > (TimeSpan)settings[Settings.WorkTime] && WorkState == State.Work) || switchState)
                    {
                        startWaitToCheckStamp           = DateTime.Now.TimeOfDay - (TimeSpan)settings[Settings.CheckTime];
                        settings[Settings.PauseInRelax] = WorkState == State.Relax;
                        if (!checkedToggle)
                        {
                            Check.Dispatcher.BeginInvoke((Action)(() => Check.IsChecked = false));
                        }
                        SwitchStateTo(State.WaitToCheck);
                        isContinueWait = true;
                        continue;
                    }

                    if (isPause)
                    {
                        settings[Settings.PauseInRelax] = WorkState == State.Relax;
                        SwitchStateTo(State.Pause);
                        settings[Settings.CurrentOffsetToPause] = offsetTime;
                        isContinueWait = true;
                        continue;
                    }

                    Label.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        Label.Content = (WorkState == State.Work ? "Work: " : "Relax: ") + offsetTime.ToString("h\\:mm\\:ss");
                        Label.Foreground = WorkState == State.Work ? Brushes.CornflowerBlue : Brushes.GreenYellow;
                    }));
                    break;

                case State.WaitToCheck:
                    Label.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        Label.Content = "WaitToCheck";
                        Label.Foreground = Brushes.DarkOrchid;
                    }));

                    if (checkedToggle)
                    {
                        SwitchStateTo(!(bool)settings[Settings.PauseInRelax] ? State.Relax : State.Work);
                        switchState    = false;
                        isContinueWait = true;
                        continue;
                    }
                    else
                    {
                        if (DateTime.Now.TimeOfDay - startWaitToCheckStamp > (TimeSpan)settings[Settings.CheckTime])
                        {
                            startWaitToCheckStamp = DateTime.Now.TimeOfDay;
                            if (switchState)
                            {
                                switchState    = false;
                                isContinueWait = true;
                                continue;
                            }
                            AttentSelector.getInstance().ShowWindow(Dispatcher);
                            //Console.WriteLine("ATTENTION");
                        }
                    }

                    break;

                case State.Pause:
                    Label.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        Label.Content = "Pause";
                        Label.Foreground = Brushes.Brown;
                    }));


                    if (!isPause)
                    {
                        SwitchStateTo((bool)settings[Settings.PauseInRelax] ? State.Relax : State.Work);
                        settings[Settings.StartTime] = DateTime.Now - (TimeSpan)settings[Settings.CurrentOffsetToPause];
                        isContinueWait = true;
                        continue;
                    }
                    break;
                }
                Dispatcher.BeginInvoke((Action)(() => { Topmost = false; }));
                Dispatcher.BeginInvoke((Action)(() => { Topmost = true; }));
            }
        }