<commit_before>﻿using System;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace WakaTime
{
    class RunProcess
    {
        private readonly string _program;
        private readonly string[] _arguments;
        private string _stdin;
        private bool _captureOutput;

        internal RunProcess(string program, params string[] arguments)
        {
            _program = program;
            _arguments = arguments;
            _captureOutput = true;
        }

        internal void RunInBackground()
        {
            _captureOutput = false;
            Run();
        }

        internal void RunInBackground(string stdin)
        {
            _captureOutput = false;
            _stdin = stdin;
            Run();
        }

        internal void Run(string stdin)
        {
            _stdin = stdin;
            Run();
        }

        internal string Output { get; private set; }

        internal string Error { get; private set; }

        internal bool Success
        {
            get { return Exception == null; }
        }

        internal Exception Exception { get; private set; }        

        internal void Run()
        {
            try
            {
                var procInfo = new ProcessStartInfo
                {
                    UseShellExecute = false,
                    RedirectStandardError = _captureOutput,
                    RedirectStandardOutput = _captureOutput,
                    RedirectStandardInput = _stdin != null,
                    FileName = _program,
                    CreateNoWindow = true,
                    Arguments = GetArgumentString(),
                };
                
                using (var process = Process.Start(procInfo))
                {
                    // run background process at lower priority to prevent lagging GUI
                    process.PriorityClass = ProcessPriorityClass.BelowNormal;

                    if (_stdin != null)
                    {
                        process.StandardInput.WriteLine(string.Format("{0}\n", _stdin));
                    }

                    if (_captureOutput)
                    {
                        var stdOut = new StringBuilder();
                        var stdErr = new StringBuilder();

                        while (process != null && !process.HasExited)
                        {
                            stdOut.Append(process.StandardOutput.ReadToEnd());
                            stdErr.Append(process.StandardError.ReadToEnd());
                        }

                        if (process != null)
                        {
                            stdOut.Append(process.StandardOutput.ReadToEnd());
                            stdErr.Append(process.StandardError.ReadToEnd());<commit_msg>ignore exceptions when setting process priority<commit_after>﻿using System;
using System.Collections;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace WakaTime
{
    class RunProcess
    {
        private readonly string _program;
        private readonly string[] _arguments;
        private string _stdin;
        private bool _captureOutput;

        internal RunProcess(string program, params string[] arguments)
        {
            _program = program;
            _arguments = arguments;
            _captureOutput = true;
        }

        internal void RunInBackground()
        {
            _captureOutput = false;
            Run();
        }

        internal void RunInBackground(string stdin)
        {
            _captureOutput = false;
            _stdin = stdin;
            Run();
        }

        internal void Run(string stdin)
        {
            _stdin = stdin;
            Run();
        }

        internal string Output { get; private set; }

        internal string Error { get; private set; }

        internal bool Success
        {
            get { return Exception == null; }
        }

        internal Exception Exception { get; private set; }        

        internal void Run()
        {
            try
            {
                var procInfo = new ProcessStartInfo
                {
                    UseShellExecute = false,
                    RedirectStandardError = _captureOutput,
                    RedirectStandardOutput = _captureOutput,
                    RedirectStandardInput = _stdin != null,
                    FileName = _program,
                    CreateNoWindow = true,
                    Arguments = GetArgumentString(),
                };
                
                using (var process = Process.Start(procInfo))
                {
                    // run background process at lower priority to prevent lagging GUI
                    try
                    {
                        process.PriorityClass = ProcessPriorityClass.BelowNormal;
                    }
                    catch (Exception pass) { /* Might fail if not Admin */ }

                    if (_stdin != null)
                    {
                        process.StandardInput.WriteLine(string.Format("{0}\n", _stdin));
                    }

                    if (_captureOutput)
                    {
                        var stdOut = new StringBuilder();
                        var stdErr = new StringBuilder();

                        while (process != null && !process.HasExited)
                        {
                            stdOut.Append(process.StandardOutput.ReadToEnd());
                            stdErr.Append(process.StandardError.ReadToEnd());
                        }

                        if (process != null)
                        {
                            stdOut.Append(process.StandardOutput.ReadToEnd());
                            stdErr.Append(process.StandardError.ReadToEnd());
{'fraction_non_alphanumeric': 0.06987497859222469, 'fraction_numerical': 0.0003425244048638465, 'mean_word_length': 1.168585220943186, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7210776', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1204, 'n_words': 317}