コード例 #1
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;
            }
        }
コード例 #2
0
    public static Process StartProcess(string fileName, string arguments, bool useShell = false, bool redirectOutput = false, string workingDirectory = null, string tag = null, bool logOut = false, bool asyncOutput = true)
    {
        var process = new Process();

        process.StartInfo = ProcessInfoFactory.Create(fileName, arguments, useShell, redirectOutput, workingDirectory);
        var logPrefix = tag == null ? "" : $"[{tag}] ";

        process.Start();
        if (redirectOutput)
        {
            DigestStdOut(process, l => HandleOutput(LogType.Log, l), asyncOutput);
            DigestStdErr(process, l => HandleOutput(LogType.Error, l), asyncOutput);
        }

        void HandleOutput(LogType logType, string line)
        {
            if (logOut && line != null)
            {
                Console.WriteLine($"{logType} - {logPrefix}{line}");
            }
        }

        return(process);
    }