Пример #1
0
 protected override async Task ExecuteAsync(CancellationToken stoppingToken)
 {
     while (!stoppingToken.IsCancellationRequested)
     {
         var activeProcess = ProcessUtils.GetActiveProcess();
         _logger.LogInformation($"Worker running at: {DateTimeOffset.Now}, active process: {activeProcess.ProcessName}");
         await Task.Delay(1000, stoppingToken);
     }
 }
Пример #2
0
        private void MonitorUserActiveProcess(object state)
        {
            if (DateTime.Now.Date != _bufferedTodayAppRecord.Date)
            {
                _bufferedTodayAppRecord = new AppUsageRecord(DateTime.Now);
            }

            if (this._isIdle = IsUserIdle())
            {
                return;
            }

            var activeProcess = ProcessUtils.GetActiveProcess();

            if (activeProcess != null)
            {
                var processInfoId = ProcessInfoFactory.CreateId(activeProcess);
                var activeApps    = _bufferedTodayAppRecord.ActiveApps;

                ProcessInfo processInfo = null;
                if (!string.IsNullOrWhiteSpace(processInfoId) && activeApps.ContainsKey(processInfoId))
                {
                    processInfo = activeApps[processInfoId];
                    processInfo.TotalAmountOfTime += TimeSpan.FromSeconds(Settings.MonitorInterval);
                }
                else
                {
                    processInfo = ProcessInfoFactory.Create(activeProcess, Settings.MonitorInterval);
                    activeApps.Add(processInfo.Id, processInfo);
                }

                if (_currentProcessId != processInfoId)
                {
                    if (!string.IsNullOrEmpty(_currentProcessId))
                    {
                        var oldProcessInfo = activeApps[_currentProcessId];
                        oldProcessInfo.EndCurrentSession(DateTime.Now.TimeOfDay);
                    }

                    processInfo.StartNewSession(DateTime.Now.TimeOfDay);
                }

                _currentProcessId = processInfoId;
            }
        }