Exemplo n.º 1
0
        public WarningWindow(uint memoryExceededThreshold, WarningType warningType, UserSettings userSettings)
        {
            InitializeComponent();
            this.userSettings            = userSettings;
            killMode                     = (warningType == WarningType.kill);
            this.memoryExceededThreshold = memoryExceededThreshold;
            Icon = SharedStatics.ToImageSource(Properties.Resources.bars_white);

            //Formatter helper for the table settings, will likely expand in the future.
            memoryHogsFormatter = new TableFormatter(memoryHogs);

            //Set the GUI labels
            memoryValue.Content = memoryExceededThreshold;
            SetSystemMemoryPercentAndLabel();

            //Build the table with data
            RefreshProcessTable(userSettings.warningWindowProcessMin, userSettings.warningWindowProcessMax, userSettings.warningWindowProcessPercentMin);
            memoryHogs.ItemsSource = processTable;

            //Enable live sorting in the window too, if data updates but I don't resort
            memoryHogs.Items.SortDescriptions.Add(new SortDescription("ramPercent", ListSortDirection.Descending));
            memoryHogs.Items.IsLiveSorting = true;

            //Be passive/aggressive/kill
            if (warningType == WarningType.passive)
            {
                SharedStatics.FlashWindow(this, SharedStatics.FLASHW_ALL | SharedStatics.FLASHW_TIMERNOFG, 1, 1);
            }
            else if (warningType == WarningType.aggressive)
            {
                this.Show();
                this.Activate();
                this.Topmost = true;
                SharedStatics.FlashWindow(this, SharedStatics.FLASHW_ALL, flashCount: 3);
            }
            else if (warningType == WarningType.kill)
            {
                //Will do the kill tasks in the next timer cycle so this constructor can finish
                this.Show();
                this.Activate();
                this.Topmost = true;
                SharedStatics.FlashWindow(this, SharedStatics.FLASHW_ALL, flashRate: 200, flashCount: 6);
            }

            //Start the update timer
            refreshTimer          = new System.Windows.Forms.Timer();
            refreshTimer.Tick    += RefreshTimer_Tick;
            refreshTimer.Interval = 1000;
            refreshTimer.Start();
        }
Exemplo n.º 2
0
        public MainWindow()
        {
            InitializeComponent();
            Icon              = SharedStatics.ToImageSource(Properties.Resources.bars_white);
            this.DataContext  = this;
            badCellBackground = new SolidColorBrush(Color.FromArgb(128, 255, 0, 0));

            //Create right click menu for the tray icon
            System.Windows.Forms.ContextMenu trayIconRightClick = new System.Windows.Forms.ContextMenu();
            System.Windows.Forms.MenuItem    trayMenuSettings   = new System.Windows.Forms.MenuItem("Settings");
            trayMenuSettings.Click += TrayMenuSettings_Click;
            System.Windows.Forms.MenuItem trayMenuQuit = new System.Windows.Forms.MenuItem("Quit");
            trayMenuQuit.Click += TrayMenuQuit_Click;
            trayIconRightClick.MenuItems.Add(trayMenuSettings);
            trayIconRightClick.MenuItems.Add(trayMenuQuit);

            //Create tray icon
            trayIcon                    = new System.Windows.Forms.NotifyIcon();
            trayIcon.Text               = "Memory Warden";
            trayIcon.Icon               = Properties.Resources.bars_white;
            trayIcon.MouseClick        += trayIconClicked;
            trayIcon.Visible            = true;
            trayIcon.BalloonTipClicked += TrayIcon_BalloonTipClicked;
            trayIcon.ContextMenu        = trayIconRightClick;

            //// Programmatically build warnings table for user to modify

            //Column: warning type
            DataGridComboBoxColumn warningTypeColumn = new DataGridComboBoxColumn();

            warningTypeColumn.Header      = "Warning Type";
            warningTypeColumn.ItemsSource = Enum.GetNames(typeof(WarningType));
            Binding warningTypeColumnBind = new Binding("typeText");

            warningTypeColumnBind.Mode = BindingMode.TwoWay;
            warningTypeColumnBind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            warningTypeColumn.SelectedItemBinding     = warningTypeColumnBind;
            warningTypeColumn.CellStyle = (Style)Resources["warningsTypeCell"];

            //Column: treshold value
            DataGridTextColumn thresholdValueColumn = new DataGridTextColumn();

            thresholdValueColumn.Header        = "Warning triggers at this memory %";
            thresholdValueColumn.SortDirection = ListSortDirection.Ascending;//Won't actually sort by this column, just here for looks
            Binding thresholdValueColumnBind = new Binding("thresholdText");

            thresholdValueColumnBind.Mode = BindingMode.TwoWay;
            thresholdValueColumnBind.UpdateSourceTrigger = UpdateSourceTrigger.LostFocus;
            thresholdValueColumn.Binding   = thresholdValueColumnBind;
            thresholdValueColumn.CellStyle = (Style)Resources["warningsThresholdCell"];

            //Add columns in desired order
            warningsDataGrid.Columns.Clear();
            warningsDataGrid.Columns.Add(warningTypeColumn);
            warningsDataGrid.Columns.Add(thresholdValueColumn);

            //Add initial rows
            warnings = new ObservableCollection <WarningEvent>();
            warnings.Add(new WarningEvent("35", WarningType.aggressive));
            warnings.Add(new WarningEvent("75", WarningType.passive));
            warnings.Add(new WarningEvent("85", WarningType.aggressive));
            warnings.Add(new WarningEvent("92", WarningType.aggressive));
            warnings.Add(new WarningEvent("98", WarningType.kill));
            warningsDataGrid.ItemsSource = warnings;

            //Enable sorting on the numeric value of threshold
            warningsDataGrid.Items.SortDescriptions.Add(new SortDescription("threshold", ListSortDirection.Ascending));
            warningsDataGrid.Items.IsLiveSorting = true;

            //Table formatter helper, to store the settings for event handlers
            //Will likely expand in the future.
            warningsDataGridFormatter = new TableFormatter(warningsDataGrid);

            //Fill in default user settings
            userSettings = new UserSettings();
            warningResetThresholdTextBox.Text          = userSettings.warningResetThreshold.ToString();
            warningWindowProcessMinTextBox.Text        = userSettings.warningWindowProcessMin.ToString();
            warningWindowProcessMaxTextBox.Text        = userSettings.warningWindowProcessMax.ToString();
            warningWindowProcessPercentMinTextBox.Text = userSettings.warningWindowProcessPercentMin.ToString();

            //Check memory and start timer for GUI, while waiting for user to press OK
            //for updating currentMemoryUsageBar
            waitingForUserTimer          = new System.Windows.Forms.Timer();
            waitingForUserTimer.Interval = 1000;        //Ignore user frequency in this window
            waitingForUserTimer.Tick    += CheckMemoryWhileWaitingForUser;
            CheckMemoryWhileWaitingForUser(this, null); //Call once so user doesn't wait
            waitingForUserTimer.Start();
        }