/// <summary> /// writes into the ActivityDB.db the activity line /// </summary> /// <param name="activity"></param> public static void saveActivityLine(ActivityLine activity) { using (IDbConnection cnn = new SQLiteConnection(connectionStringActivity)) { cnn.Execute("insert into Activity (Date,Time,FGWindowName,FGProcessName,inAppTime,Tag) values (@Date,@Time,@FGWindowName,@FGProcessName,@inAppTime,@Tag)", activity); } }
/// <summary> /// updates the previousActivity and the current Tag and saves the activity in the database /// </summary> /// <param name="e"></param> public void UpdatePreviousActivity(string MainWindowTitle, string ProcessName) { //set the previous time span previousActivity.inAppTime = DateTime.Now.Subtract(previousActivity.DateAndTime); SqliteDataAccess.saveActivityLine(previousActivity); previousActivity = new ActivityLine(DateTime.Now, MainWindowTitle, ProcessName); mainViewModel.currentTag = previousActivity.Tag; }
/// <summary> /// updates the previousActivity and the current Tag and saves the activity in the database /// </summary> /// <param name="e"></param> public void UpdatePreviousActivity(ActivityLine activity) { //set the previous time span previousActivity.inAppTime = DateTime.Now.Subtract(previousActivity.DateAndTime); SqliteDataAccess.saveActivityLine(previousActivity); previousActivity = activity; previousActivity.SetDateAndTime(DateTime.Now); mainViewModel.currentTag = previousActivity.Tag; }
public static (string, Brush) getTag(ActivityLine line) { for (int i = 0; i < ruleFunctions.Count; i++) { if (ruleFunctions[i](line)) { return(tagList[ruleList[i].TagId].TagName, tagList[ruleList[i].TagId].TagColor); } } return("null", Brushes.Black); }
/// <summary> /// Set Up the MainBackground Logic. /// The hard part to understand from my point of view is the BackgroundWorker. /// </summary> /// <param name="mainViewModel"></param> public MainBackgroundLogic(MainViewModel mainViewModel) { this.mainViewModel = mainViewModel; var tagModel = mainViewModel.TagViewModel; previousActivity = new ActivityLine(Process.GetCurrentProcess().StartTime, Process.GetCurrentProcess().MainWindowTitle, Process.GetCurrentProcess().ProcessName); tagModel.ActivityTitle = previousActivity.ToTitle(); currentDate = DateTime.Today; dayCounter = 0; counter = 0; managerWindowChangedWorker = new BackgroundWorker(); // so i want to send off a notification from my main thread (where the hooks are) to the background thread. // The background thread will make all the checks and database connections and will send back the results // The results being the properties to update and with what. managerWindowChangedWorker.WorkerReportsProgress = true; managerWindowChangedWorker.DoWork += ManagerWindowChangedWorker_DoWork; managerWindowChangedWorker.RunWorkerCompleted += ManagerWindowChangedWorker_RunWorkerCompleted; managerWindowChangedWorker.ProgressChanged += ManagerWindowChangedWorker_ProgressChanged; }
/// <summary> /// The hard wor that is done in a seperate thread by the background worker. /// The hard lifting as I see it is the getForegroundProcess(); that is called from the [DllImport("user32.dll")]. /// After that the application saves the activity into the database. /// This has to be in the background as to not hold up the Hooks. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ManagerWindowChangedWorker_DoWork(object sender, DoWorkEventArgs e) { var process = getForegroundProcess(); if (!previousActivity.FGProcessName.Equals(process.ProcessName) || !previousActivity.FGWindowName.Equals(process.MainWindowTitle)) { try { UpdatePreviousActivity(process); } catch (Exception ex) { managerWindowChangedWorker.ReportProgress(0, ex); // the number will be code for what situation it is 0 =exception } } else if (isIdle) // As i written before, i might want to change this code (see comment in the Timer_Tick - idle section 19/09/21 21:09) { ActivityLine activity = SqliteDataAccess.LoadSecondToLastActivity(); UpdatePreviousActivity(activity); managerWindowChangedWorker.ReportProgress(1, activity); } }
private void ManagerWindowChangedWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { switch (e.ProgressPercentage) { case 0: // case 0 - there is an error - if so, deal with it. Exception ex = e.UserState as Exception; MessageBox.Show(ex.ToString() + "\n \n " + ex.Message, "Hook Error!", MessageBoxButton.OK, MessageBoxImage.Error); Task LogExceptionTask = App.LogExceptions(ex, ex.Message); appClosing(); mainViewModel.SetUpHook(); LogExceptionTask.Wait(); break; case 1: // case 1 - the computer came of being idle. isIdle = false; ActivityLine activity = e.UserState as ActivityLine; mainViewModel.TagViewModel.ActivityTitle = activity.ToTitle(); break; case 2: // case 2 - there was a hook messege but no change in database. isIdle = false; break; } }