<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.System;

namespace Emby.Theater.App
{
    public class ElectronApp : IDisposable
    {
        private Process _process;
        private ILogger _logger;
        private Func<IHttpClient> _httpClient;
        private IApplicationPaths _appPaths;
        private IEnvironmentInfo _environmentInfo;

        public ElectronApp(ILogger logger, IApplicationPaths appPaths, Func<IHttpClient> httpClient, IEnvironmentInfo environment)
        {
            _logger = logger;
            _appPaths = appPaths;
            _httpClient = httpClient;
            _environmentInfo = environment;
            StartProcess();
        }

        public void Dispose()
        {
            CloseProcess();
            GC.SuppressFinalize(this);
        }

        private void StartProcess()
        {
            var appDirectoryPath = Path.GetDirectoryName(Program.ApplicationPath);

            var is64BitElectron = Program.Is64BitElectron;

            var architecture = is64BitElectron ? "x64" : "x86";
            var archPath = Path.Combine(appDirectoryPath, architecture);
            var electronExePath = Path.Combine(archPath, "electron", "electron.exe");
            var electronAppPath = Path.Combine(appDirectoryPath, "electronapp");
            var mpvExePath = Path.Combine(archPath, "mpv", "mpv.exe");

            var dataPath = Path.Combine(_appPaths.DataPath, "electron");

            var cecPath = Path.Combine(Path.GetDirectoryName(Program.ApplicationPath), "cec");
            if (is64BitElectron)
            {
                cecPath = Path.Combine(cecPath, "cec-client.x64.exe");
            }
            else
            {
                cecPath = Path.Combine(cecPath, "cec-client.exe");
            }

            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    UseShellExecute = false,

                    FileName = electronExePath,
                    Arguments = string.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\"", electronAppPath, dataPath, cecPath, mpvExePath)
                },

                EnableRaisingEvents = true,
            };

            _logger.Info("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);

            process.Start();

            //process.WaitForInputIdle(3000);

            while (process.MainWindowHandle.Equals(IntPtr.Zero))
            {
                var task = Task.Delay(50);
                Task.WaitAll(task);
            }<commit_msg>resolve shutdown not closing process<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Net;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.System;

namespace Emby.Theater.App
{
    public class ElectronApp : IDisposable
    {
        private Process _process;
        private ILogger _logger;
        private Func<IHttpClient> _httpClient;
        private IApplicationPaths _appPaths;
        private IEnvironmentInfo _environmentInfo;

        public ElectronApp(ILogger logger, IApplicationPaths appPaths, Func<IHttpClient> httpClient, IEnvironmentInfo environment)
        {
            _logger = logger;
            _appPaths = appPaths;
            _httpClient = httpClient;
            _environmentInfo = environment;
            StartProcess();
        }

        public void Dispose()
        {
            CloseProcess();
            GC.SuppressFinalize(this);
        }

        private void StartProcess()
        {
            var appDirectoryPath = Path.GetDirectoryName(Program.ApplicationPath);

            var is64BitElectron = Program.Is64BitElectron;

            var architecture = is64BitElectron ? "x64" : "x86";
            var archPath = Path.Combine(appDirectoryPath, architecture);
            var electronExePath = Path.Combine(archPath, "electron", "electron.exe");
            var electronAppPath = Path.Combine(appDirectoryPath, "electronapp");
            var mpvExePath = Path.Combine(archPath, "mpv", "mpv.exe");

            var dataPath = Path.Combine(_appPaths.DataPath, "electron");

            var cecPath = Path.Combine(Path.GetDirectoryName(Program.ApplicationPath), "cec");
            if (is64BitElectron)
            {
                cecPath = Path.Combine(cecPath, "cec-client.x64.exe");
            }
            else
            {
                cecPath = Path.Combine(cecPath, "cec-client.exe");
            }

            var process = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    UseShellExecute = false,

                    FileName = electronExePath,
                    Arguments = string.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\"", electronAppPath, dataPath, cecPath, mpvExePath)
                },

                EnableRaisingEvents = true,
            };

            _logger.Info("{0} {1}", process.StartInfo.FileName, process.StartInfo.Arguments);

            process.Exited += Process_Exited;

            process.Start();

            //process.WaitForInputIdle(3000);

            while (process.MainWindowHandle.Equals(IntPtr.Zero))
            {
                var task = Task.Delay(50);
                Task.WaitAll(task);
            }
        }

        private void Process_Exited(object sender, EventArgs e)
        {
            Program.Exit();
{'fraction_non_alphanumeric': 0.09575012800819252, 'fraction_numerical': 0.008875234681686295, 'mean_word_length': 1.9974424552429668, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '4308708', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1527, 'n_words': 315}