//to update time and reset
        private void Update_Tick(object sender, EventArgs e)
        {
            ticks++;
            if (ticks % 10 == 0)
            {
                //daily reset of usetime
                if (resetTime.Date != DateTime.Now.Date)
                {
                    resetTime = DateTime.Now;
                    foreach (Programm p in logPrograms)
                    {
                        p.setUsedTime(0);
                    }
                    foreach (Category c in logCategorys)
                    {
                        c.setUsedTime(0);
                    }
                }
                //update usetime of each category
                foreach (Category category in logCategorys)
                {
                    bool foundOne = false;
                    for (int i = 0; i < currentProgsListView.Items.Count; i++)
                    {
                        var     item    = currentProgsListView.Items[i];
                        Process process = (Process)item.Tag;
                        foreach (Programm program in category.GetPrograms())
                        {
                            try
                            {
                                if (process.MainModule.FileName.Contains(program.getPath()) && process.ProcessName == program.getName())
                                {
                                    foundOne = true;
                                    break;
                                }
                            }
                            catch { }
                        }
                        if (foundOne)
                        {
                            category.setUsedTime(category.getUsedTime() + 1);
                            if (category.getUsedTime() == (category.getMaxTime() - remainingSecondsForWarning1))
                            {
                                MessageBox.Show("Die Zeit für " + category.getName() + " ist bald aufgebraucht!", "Warnung", MessageBoxButtons.OK);
                            }
                            if (category.getUsedTime() == (category.getMaxTime() - remainingSecondsForWarning2))
                            {
                                MessageBox.Show("Programme aus " + category.getName() + " werden gleich beendet!", "Warnung", MessageBoxButtons.OK);
                            }
                            break;
                        }
                    }
                }
                for (int i = 0; i < currentProgsListView.Items.Count; i++)
                {
                    var     item    = currentProgsListView.Items[i];
                    Process process = (Process)item.Tag;

                    //usage = usetime since last reset
                    if (process.StartTime.Date == resetTime.Date)
                    {
                        usage = DateTime.Now.Subtract(process.StartTime);
                    }
                    else
                    {
                        usage = DateTime.Now.Subtract(resetTime);
                    }
                    item.SubItems[3].Text = usage.ToString(@"hh\:mm\:ss");

                    //check whether a program to be regulated is running
                    for (int j = 0; j < savedProgsListView.Items.Count; j++)
                    {
                        try
                        {
                            Programm savedProg = (Programm)savedProgsListView.Items[j].Tag;
                            if ((process.MainModule.FileName.Contains(savedProg.getPath()) || savedProg.getPath().Contains(process.MainModule.FileName)) &&
                                process.ProcessName == savedProg.getName())
                            {
                                savedProg.setUsedTime(Convert.ToInt32(usage.TotalSeconds));
                                //time warning
                                if ((savedProg.getIndividualLimit() || savedProg.getCategory() == "" || savedProg.getCategory() == null) &&
                                    savedProg.getUsedTime() == (savedProg.getMaxTime() - remainingSecondsForWarning1))
                                {
                                    String msg = "Die Zeit für " + savedProg.getName() + " ist bald aufgebraucht!";
                                    if (this.WindowState == FormWindowState.Minimized)
                                    {
                                        this.Hide();
                                        timerNotification.ShowBalloonTip(1000, "Warnung", msg, ToolTipIcon.Info);
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg, "Warnung", MessageBoxButtons.OK);
                                    }
                                }
                                if ((savedProg.getIndividualLimit() || savedProg.getCategory() == "" || savedProg.getCategory() == null) &&
                                    savedProg.getUsedTime() == (savedProg.getMaxTime() - remainingSecondsForWarning2))
                                {
                                    String msg = savedProg.getName() + " wird gleich beendet!";
                                    if (this.WindowState == FormWindowState.Minimized)
                                    {
                                        this.Hide();
                                        timerNotification.ShowBalloonTip(1000, "Warnung", msg, ToolTipIcon.Info);
                                    }
                                    else
                                    {
                                        MessageBox.Show(msg, "Warnung", MessageBoxButtons.OK);
                                    }
                                }
                                //max usetime of a program or category has reached it's limit
                                if ((savedProg.getIndividualLimit() || savedProg.getCategory() == "" || savedProg.getCategory() == null) &&
                                    savedProg.getUsedTime() >= savedProg.getMaxTime())
                                {
                                    CloseProgram(process);
                                }
                                //Maximale Nutzungszeit der ategory überschritten
                                if (savedProg.getCategory() != "" && savedProg.getCategory() != null)
                                {
                                    Category cat = logCategorys.Find(c => c.getName().Equals(savedProg.getCategory()));
                                    if (cat.getUsedTime() >= cat.getMaxTime())
                                    {
                                        CloseProgram(process);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                    }
                }

                //keep selected entry
                if (currentProgsListView.SelectedItems.Count > 0)
                {
                    string id = currentProgsListView.SelectedItems[0].SubItems[0].Text;
                    fillCurrentProgsListView();
                    foreach (ListViewItem lvi in currentProgsListView.Items)
                    {
                        if (id.Equals(lvi.SubItems[0].Text))
                        {
                            lvi.Selected = true;
                        }
                    }
                }
                else
                {
                    fillCurrentProgsListView();
                }
                if (savedProgsListView.SelectedItems.Count > 0)
                {
                    string name = savedProgsListView.SelectedItems[0].SubItems[0].Text;
                    string path = savedProgsListView.SelectedItems[0].SubItems[1].Text;
                    fillSavedProgsListView();
                    foreach (ListViewItem lvi in savedProgsListView.Items)
                    {
                        if (name.Equals(lvi.SubItems[0].Text) && path.Equals(lvi.SubItems[1].Text))
                        {
                            lvi.Selected = true;
                        }
                    }
                }
                else
                {
                    fillSavedProgsListView();
                }
                editCats.FillCategoryListView(false);
                updateDetailBox(false);
            }
        }
Example #2
0
 private void Update_Tick(object sender, EventArgs e)
 {
     ticks++;
     if (ticks % 10 == 0)
     {
         //Täglicher Reset der Nutzungszeiten
         if (resetTime.Date != DateTime.Now.Date)
         {
             resetTime = DateTime.Now;
             foreach (Programm p in logProgramme)
             {
                 p.setUsedTime(0);
             }
             foreach (Kategorie k in logKategorien)
             {
                 k.setUsedTime(0);
             }
         }
         //Nutzungszeiten der Kategorien updaten
         foreach (Kategorie kategorie in logKategorien)
         {
             bool foundOne = false;
             for (int i = 0; i < currentProgsListView.Items.Count; i++)
             {
                 var     item    = currentProgsListView.Items[i];
                 Process process = (Process)item.Tag;
                 foreach (Programm programm in kategorie.GetProgramme())
                 {
                     try
                     {
                         if (process.MainModule.FileName.Contains(programm.getPath()) && process.ProcessName == programm.getName())
                         {
                             foundOne = true;
                             break;
                         }
                     }
                     catch { }
                 }
                 if (foundOne)
                 {
                     kategorie.setUsedTime(kategorie.getUsedTime() + 1);
                     if (kategorie.getUsedTime() == (kategorie.getMaxTime() - remainingSecondsForWarning1))
                     {
                         MessageBox.Show("Die Zeit für " + kategorie.getName() + " ist bald aufgebraucht!", "Warnung", MessageBoxButtons.OK);
                     }
                     if (kategorie.getUsedTime() == (kategorie.getMaxTime() - remainingSecondsForWarning2))
                     {
                         MessageBox.Show("Programme aus " + kategorie.getName() + " werden gleich beendet!", "Warnung", MessageBoxButtons.OK);
                     }
                     break;
                 }
             }
         }
         for (int i = 0; i < currentProgsListView.Items.Count; i++)
         {
             var     item    = currentProgsListView.Items[i];
             Process process = (Process)item.Tag;
             //usage = Nutzungszeit seit letztem Reset
             if (process.StartTime.Date == resetTime.Date)
             {
                 usage = DateTime.Now.Subtract(process.StartTime);
             }
             else
             {
                 usage = DateTime.Now.Subtract(resetTime);
             }
             item.SubItems[3].Text = usage.ToString(@"hh\:mm\:ss");
             //Prüfe ob ein zu regulierendes Programm läuft
             for (int j = 0; j < savedProgsListView.Items.Count; j++)
             {
                 try
                 {
                     Programm savedProg = (Programm)savedProgsListView.Items[j].Tag;
                     if ((process.MainModule.FileName.Contains(savedProg.getPath()) || savedProg.getPath().Contains(process.MainModule.FileName)) &&
                         process.ProcessName == savedProg.getName())
                     {
                         savedProg.setUsedTime(Convert.ToInt32(usage.TotalSeconds));
                         //Zeitwarnung
                         if ((savedProg.getIndividualLimit() || savedProg.getKategorie() == "" || savedProg.getKategorie() == null) &&
                             savedProg.getUsedTime() == (savedProg.getMaxTime() - remainingSecondsForWarning1))
                         {
                             String msg = "Die Zeit für " + savedProg.getName() + " ist bald aufgebraucht!";
                             if (this.WindowState == FormWindowState.Minimized)
                             {
                                 this.Hide();
                                 timerNotification.ShowBalloonTip(1000, "Warnung", msg, ToolTipIcon.Info);
                             }
                             else
                             {
                                 MessageBox.Show(msg, "Warnung", MessageBoxButtons.OK);
                             }
                         }
                         if ((savedProg.getIndividualLimit() || savedProg.getKategorie() == "" || savedProg.getKategorie() == null) &&
                             savedProg.getUsedTime() == (savedProg.getMaxTime() - remainingSecondsForWarning2))
                         {
                             String msg = savedProg.getName() + " wird gleich beendet!";
                             if (this.WindowState == FormWindowState.Minimized)
                             {
                                 this.Hide();
                                 timerNotification.ShowBalloonTip(1000, "Warnung", msg, ToolTipIcon.Info);
                             }
                             else
                             {
                                 MessageBox.Show(msg, "Warnung", MessageBoxButtons.OK);
                             }
                         }
                         //Maximale Nutzungszeit des Programms überschritten
                         if ((savedProg.getIndividualLimit() || savedProg.getKategorie() == "" || savedProg.getKategorie() == null) &&
                             savedProg.getUsedTime() >= savedProg.getMaxTime())
                         {
                             CloseProgram(process);
                         }
                         //Maximale Nutzungszeit der Kategorie überschritten
                         if (savedProg.getKategorie() != "" && savedProg.getKategorie() != null)
                         {
                             Kategorie kat = logKategorien.Find(k => k.getName().Equals(savedProg.getKategorie()));
                             if (kat.getUsedTime() >= kat.getMaxTime())
                             {
                                 CloseProgram(process);
                             }
                         }
                     }
                 }
                 catch (Exception ex)
                 {
                     Console.WriteLine(ex.Message);
                 }
             }
         }
         //Ausgewählten Eintrag beibehalten
         if (currentProgsListView.SelectedItems.Count > 0)
         {
             string id = currentProgsListView.SelectedItems[0].SubItems[0].Text;
             fillCurrentProgsListView();
             foreach (ListViewItem lvi in currentProgsListView.Items)
             {
                 if (id.Equals(lvi.SubItems[0].Text))
                 {
                     lvi.Selected = true;
                 }
             }
         }
         else
         {
             fillCurrentProgsListView();
         }
         if (savedProgsListView.SelectedItems.Count > 0)
         {
             string name = savedProgsListView.SelectedItems[0].SubItems[0].Text;
             string path = savedProgsListView.SelectedItems[0].SubItems[1].Text;
             fillSavedProgsListView();
             foreach (ListViewItem lvi in savedProgsListView.Items)
             {
                 if (name.Equals(lvi.SubItems[0].Text) && path.Equals(lvi.SubItems[1].Text))
                 {
                     lvi.Selected = true;
                 }
             }
         }
         else
         {
             fillSavedProgsListView();
         }
         editKats.FillListView(false);
         updateDetailBox(false);
     }
 }