diff --git a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml index fccccfdf3c52b8beec471932468ebe8a20b4f3f2..5ae7dcb57ed279a91ac932788e8df8a62da31542 100644 --- a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml +++ b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml @@ -53,8 +53,8 @@ </StackPanel> <Button Content="Pause" Name="PlayPauseButton" Style="{StaticResource PlayPauseButton}" HorizontalAlignment="Center" Margin="0,320,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Click="PlayPause_Click"></Button> - <Button Content="Mascott Up" Click="Button_Click_1" HorizontalAlignment="Left" Margin="116,375,0,0" VerticalAlignment="Top"/> - <Button Content="Mascott Down" Click="Button_Click" HorizontalAlignment="Left" Margin="116,400,0,0" VerticalAlignment="Top"/> + <!--<Button Content="Mascott Up" Click="Button_Click_1" HorizontalAlignment="Left" Margin="116,375,0,0" VerticalAlignment="Top"/> + <Button Content="Mascott Down" Click="Button_Click" HorizontalAlignment="Left" Margin="116,400,0,0" VerticalAlignment="Top"/>--> <TextBlock HorizontalAlignment="Left" Margin="116,350,0,0" Text="0" VerticalAlignment="Top" Name="StageText"/> </Grid> </Page> \ No newline at end of file diff --git a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml.cs b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml.cs index e05d3cfa94a0910d7566fe951ed8bc45e996c2e7..26ba617d6b3e69f1c2cab5024c65312461e3f1fc 100644 --- a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml.cs +++ b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml.cs @@ -3,6 +3,7 @@ using InnoLabProjektDektopApp.Services; using System.Windows; using System.Windows.Controls; using System.Windows.Media; +using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Threading; @@ -308,10 +309,17 @@ namespace InnoLabProjektDektopApp private void Page_Unloaded(object sender, RoutedEventArgs e) { + timer.Stop(); + timer.Tick -= Timertick; + processMonitor.MonitorCycleStarted -= HandleMonitorCycleStarted; + processMonitor.MonitorPaused -= HandleMonitorPaused; + processMonitor.MonitorResumed -= HandleMonitorResumed; + processMonitor.MonitorBreakStarted -= HandleMonitorBreakStarted; + processMonitor.MonitorSessionFinished -= HandleMonitorSessionFinished; mascott.Close(); } - private void Button_Click_1(object sender, RoutedEventArgs e) + /*private void Button_Click_1(object sender, RoutedEventArgs e) { mascott.emotionUp_(); ProcessMonitor.tmpStage++; @@ -323,7 +331,7 @@ namespace InnoLabProjektDektopApp mascott.emotionDown_(); ProcessMonitor.tmpStage--; StageText.Text = ProcessMonitor.tmpStage.ToString(); - } + }*/ private void Button_Click_2(object sender, RoutedEventArgs e) { diff --git a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/Mascott.xaml b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/Mascott.xaml index 34c264f568af9e040479bd07ca84e54c90fddb91..343a8c859a9bdea9ca8e3cc14426714fe37353ec 100644 --- a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/Mascott.xaml +++ b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/Mascott.xaml @@ -17,7 +17,9 @@ Title="Mascott" Height="250" Width="250"> <Grid> <Image Name="MascottImage" Source="pack://application:,,,/Assets/MascottAnimation/Up/4/frame0.png" Width="250" Height="250" /> + <!-- <TextBlock Name="Counterxd" HorizontalAlignment="Left" Height="30" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="55" Foreground="White" FontSize="22" Margin="195,45,0,0"/> <Ellipse Name="ellipse1" HorizontalAlignment="Left" Fill="Lime" Height="28" Margin="208,0,0,0" Stroke="Black" VerticalAlignment="Center" Width="28" RenderTransformOrigin="-0.946,0.07"/> + --> </Grid> </Window> diff --git a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/Mascott.xaml.cs b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/Mascott.xaml.cs index 0b2058e38cd48cb0613af5a88f820e845c151504..87b013deaf0c17eaee67cc6a3094fd963298c9a4 100644 --- a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/Mascott.xaml.cs +++ b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/Mascott.xaml.cs @@ -59,18 +59,18 @@ namespace InnoLabProjektDektopApp.Screens.Regulaer if (processMonitorStage == oldDistractionStage) { - Counterxd.Text = "x: " + processMonitorStage.ToString(); + // Counterxd.Text = "x: " + processMonitorStage.ToString(); return; } else if (processMonitorStage < oldDistractionStage) { emotionUp_(); - Counterxd.Text = "d: " + processMonitorStage.ToString(); + // Counterxd.Text = "d: " + processMonitorStage.ToString(); } else if (processMonitorStage > oldDistractionStage) { emotionDown_(); - Counterxd.Text = "u: " + processMonitorStage.ToString(); + // Counterxd.Text = "u: " + processMonitorStage.ToString(); } oldDistractionStage = processMonitorStage; } @@ -86,7 +86,7 @@ namespace InnoLabProjektDektopApp.Screens.Regulaer animationDirection = "Down"; frameCounter = 1; - Counterxd.Text = distractionStage.ToString(); + // Counterxd.Text = distractionStage.ToString(); frameTimer = new DispatcherTimer(); frameTimer.Interval = TimeSpan.FromMilliseconds(42); frameTimer.Tick += frameTick; @@ -104,7 +104,7 @@ namespace InnoLabProjektDektopApp.Screens.Regulaer animationDirection = "Up"; frameCounter = 1; - Counterxd.Text = distractionStage.ToString(); + // Counterxd.Text = distractionStage.ToString(); frameTimer = new DispatcherTimer(); frameTimer.Interval = TimeSpan.FromMilliseconds(42); frameTimer.Tick += frameTick; @@ -120,12 +120,12 @@ namespace InnoLabProjektDektopApp.Screens.Regulaer if (!successfullySet) { frameTimer.Stop(); - ellipse1.Fill = Brushes.Red; + //ellipse1.Fill = Brushes.Red; } - else + /* else { ellipse1.Fill = Brushes.Lime; - } + }*/ frameCounter++; } diff --git a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/ProcessMonitor.cs b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/ProcessMonitor.cs index da09c2656611d96ac655cea4e4461fa9d1fc547f..b5467771ed9b97270ef6d0a062b0f20305e70f4f 100644 --- a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/ProcessMonitor.cs +++ b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Services/ProcessMonitor.cs @@ -2,6 +2,7 @@ using System.IO; using System.Management; using System.Runtime; +using System.Threading.Tasks; using System.Windows; using System.Windows.Navigation; using Newtonsoft.Json; @@ -46,12 +47,17 @@ namespace InnoLabProjektDektopApp.Services public event EventHandler MonitorResumed; public event EventHandler MonitorBreakStarted; public event EventHandler MonitorSessionFinished; + private ManagementEventWatcher watcher; public ProcessMonitor() { IsBreak = false; IsPause = false; blockedProcesses = LoadBlockedProcesses(); + var query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 1), + "TargetInstance isa \"Win32_Process\""); + watcher = new ManagementEventWatcher { Query = query }; + watcher.EventArrived += (sender, e) => HandleProcessEvent(e.NewEvent); } public async void StartMonitoring() @@ -66,11 +72,9 @@ namespace InnoLabProjektDektopApp.Services cycleStartTimeList[1] = DateTime.Now; } Debug.WriteLine("Started Process Monitoring"); - var query = new WqlEventQuery("__InstanceCreationEvent", new TimeSpan(0, 0, 1), - "TargetInstance isa \"Win32_Process\""); - var watcher = new ManagementEventWatcher { Query = query }; isMonitoring = true; + await Task.Run(() => watcher.Start()); // check if any distracting processes are already running var processes = GetRunningProcessList(); @@ -81,22 +85,16 @@ namespace InnoLabProjektDektopApp.Services ShowDistractionWarning(process.Key); } } + } - - await Task.Run(() => + private void HandleProcessEvent(ManagementBaseObject e) + { + if(!isMonitoring) { - while (isMonitoring) - { - // this wait is here to prevent another event from being triggered before the previous one is handled - - var task = Task.Run(() => watcher.WaitForNextEvent()); - if (Task.WaitAny(task, Task.Delay(100)) == 0) - { - if (!isMonitoring) break; - Task.Run(() => HandleNewProcess(task.Result)); - } - } - }); + return; + } + Task.Run(() => HandleNewProcess(e)); + } public void StopMonitoring() @@ -105,6 +103,7 @@ namespace InnoLabProjektDektopApp.Services { return; } + watcher.Stop(); Debug.WriteLine("Stopping Process Monitoring"); foreach(var process in processStartTimes) { @@ -135,9 +134,9 @@ namespace InnoLabProjektDektopApp.Services } breakInfoList.Add(new BreakInfo(breakStartTime, DateTime.Now, IsBreak, currentCycle)); IsBreak = false; - StartMonitoring(); currentCycle++; cycleStartTimeList[currentCycle] = DateTime.Now; + StartMonitoring(); MonitorCycleStarted?.Invoke(this, EventArgs.Empty); } @@ -209,11 +208,12 @@ namespace InnoLabProjektDektopApp.Services return sessionInfoFilePath; } + /* public static int tmpStage = 4; public static int CalculateCurrentDistractionStageTmp(DateTime endTime) { return tmpStage; - } + }*/ public int CalculateCurrentDistractionStage(DateTime endTime) { @@ -255,11 +255,9 @@ namespace InnoLabProjektDektopApp.Services { // Check if the process is already being tracked if (processStartTimes.TryGetValue(processName, out _)) return; - - processStartTimes[processName] = DateTime.Now; - if (IsProcessOnBlockedList(processName)) { + processStartTimes[processName] = DateTime.Now; // Add the process to the list of tracked to prevent multiple warnings var result = MessageBox.Show($"Are you sure you want to get distracted by {processName}?", "Distracting Process Detected", MessageBoxButton.YesNo, MessageBoxImage.Question, @@ -274,6 +272,8 @@ namespace InnoLabProjektDektopApp.Services } else { + // Save the start time of the process so we can calculate the time it has been running without waiting for the process to end + processStartTimes[processName] = DateTime.Now; TrackProcess(processName, DateTime.Now); } } @@ -340,7 +340,6 @@ namespace InnoLabProjektDektopApp.Services var waitForExitTask = Task.Run(() => { process.WaitForExit(); - SaveTrackedData(processName, startTime); }, token); // Task to monitor the isMonitoring flag @@ -357,6 +356,7 @@ namespace InnoLabProjektDektopApp.Services // Await the completion of either the waitForExitTask or the monitorTask await Task.WhenAny(waitForExitTask, monitorTask); cts.Cancel(); + SaveTrackedData(processName, startTime); } catch (OperationCanceledException) {