<commit_before>using System;
using System.Threading;
using Alphora.Dataphor.DAE;
using Alphora.Dataphor.DAE.Client;
using Alphora.Dataphor.Frontend.Client.Windows;

namespace Alphora.Dataphor.Dataphoria.TextEditor
{
    /// <summary> Asyncronously executes queries. </summary>
    /// <remarks> Should not be used multiple times (create another instance). </remarks>
    internal class ScriptExecutor : Object
    {
        public const int CStopTimeout = 10000; // ten seconds to synchronously stop
        private Thread FAsyncThread;

        private ExecuteFinishedHandler FExecuteFinished;
        private ReportScriptProgressHandler FExecuteProgress;
        private bool FIsRunning = false;
        private IServerProcess FProcess;

        private int FProcessID;
        private string FScript;
        private IServerSession FSession;

        public ScriptExecutor(IServerSession ASession, String AScript, ReportScriptProgressHandler AExecuteProgress,
                              ExecuteFinishedHandler AExecuteFinished)
        {
            FSession = ASession;
            FScript = AScript;
            FExecuteFinished = AExecuteFinished;
            FExecuteProgress = AExecuteProgress;
        }

        public bool IsRunning
        {
            get { return FIsRunning; }
        }

        public void Start()
        {
            if (!FIsRunning)
            {
                CleanupProcess();
                FProcess = FSession.StartProcess(new ProcessInfo(FSession.SessionInfo));
                FProcessID = FProcess.ProcessID;
                FIsRunning = true;
                FAsyncThread = new Thread(ExecuteAsync);
                FAsyncThread.Start();
            }
        }

        private void CleanupProcess()
        {
            if (FProcess != null)
            {
                try
                {
                    FSession.StopProcess(FProcess);
                }
                catch
                {
                    // Don't rethrow, the session may have already been stopped
                }
                FProcess = null;
                FProcessID = 0;
            }
        }

        public void Stop()
        {
            if (FIsRunning)
            {
                FIsRunning = false;

                // Asyncronously request that the server process be stopped
                new AsyncStopHandler(AsyncStop).BeginInvoke(FProcessID, FSession, null, null);
                FProcess = null;
                FProcessID = 0;
            }
        }

        private void AsyncStop(int AProcessID, IServerSession ASession)
        {
            IServerProcess LProcess = ASession.StartProcess(new ProcessInfo(FSession.SessionInfo));
            try
            {
                LProcess.Execute("StopProcess(" + AProcessID + ")", null);
            }
            finally
            {
                ASession.StopProcess(LProcess);
            }
        }

        private void ExecuteAsync()
        {
            try
            {
                ErrorList LErrors = null;
                TimeSpan LElapsed = TimeSpan.Zero;
                

                try
                {
                    ScriptExecutionUtility.ExecuteScript
                        (
                        FProcess,
                        FScript,
                        ScriptExecuteOption.All,
                        out LErrors,
                        out LElapsed,
                        (AStatistics, AResults) => Session.SafelyInvoke(new ReportScriptProgressHandler(AsyncProgress),
                                                                        new object[] {AStatistics, AResults})
                        );
                }
                finally
                {
                    Session.SafelyInvoke(new ExecuteFinishedHandler(AsyncFinish), new object[] {LErrors, LElapsed});
                }
            }
            catch
            {
                // Don't allow exceptions to go unhandled... the framework will abort the application
            }
        }

        private void AsyncProgress(PlanStatistics AStatistics, string AResults)
        {
            // Return what results we got even if stopped.
            FExecuteProgress(AStatistics, AResults);
        }

        private void AsyncFinish(ErrorList AErrors, TimeSpan AElapsedTime)
        {
            CleanupProcess();
            if (FIsRunning)
                FExecuteFinished(AErrors, AElapsedTime);
        }

        #region Nested type: AsyncStopHandler

        private delegate void AsyncStopHandler(int AProcessID, IServerSession ASession);

        #endregion
    }<commit_msg>using the new logging api where the exeptions were just being swallowed<commit_after>using System;
using System.Diagnostics;
using System.Threading;
using Alphora.Dataphor.DAE;
using Alphora.Dataphor.DAE.Client;
using Alphora.Dataphor.Frontend.Client.Windows;
using Alphora.Dataphor.Logging;

namespace Alphora.Dataphor.Dataphoria.TextEditor
{
    /// <summary> Asyncronously executes queries. </summary>
    /// <remarks> Should not be used multiple times (create another instance). </remarks>
    internal class ScriptExecutor : Object
    {

        static readonly ILogger SRFLogger = LoggerFactory.Instance.CreateLogger(typeof(ScriptExecutor));
        
        public const int CStopTimeout = 10000; // ten seconds to synchronously stop
        private Thread FAsyncThread;

        private ExecuteFinishedHandler FExecuteFinished;
        private ReportScriptProgressHandler FExecuteProgress;
        private bool FIsRunning = false;
        private IServerProcess FProcess;

        private int FProcessID;
        private string FScript;
        private IServerSession FSession;

        public ScriptExecutor(IServerSession ASession, String AScript, ReportScriptProgressHandler AExecuteProgress,
                              ExecuteFinishedHandler AExecuteFinished)
        {
            FSession = ASession;
            FScript = AScript;
            FExecuteFinished = AExecuteFinished;
            FExecuteProgress = AExecuteProgress;
        }

        public bool IsRunning
        {
            get { return FIsRunning; }
        }

        public void Start()
        {
            if (!FIsRunning)
            {
                CleanupProcess();
                FProcess = FSession.StartProcess(new ProcessInfo(FSession.SessionInfo));
                FProcessID = FProcess.ProcessID;
                FIsRunning = true;
                FAsyncThread = new Thread(ExecuteAsync);
                FAsyncThread.Start();
            }
        }

        private void CleanupProcess()
        {
            if (FProcess != null)
            {
                try
                {
                    FSession.StopProcess(FProcess);
                }
                catch(Exception LException)
                {
                    SRFLogger.WriteLine(TraceLevel.Error, "Exception at CleanupProcess {0}", LException);
                    // Don't rethrow, the session may have already been stopped
                }
                FProcess = null;
                FProcessID = 0;
            }
        }

        public void Stop()
        {
            if (FIsRunning)
            {
                FIsRunning = false;

                // Asyncronously request that the server process be stopped
                new AsyncStopHandler(AsyncStop).BeginInvoke(FProcessID, FSession, null, null);
                FProcess = null;
                FProcessID = 0;
            }
        }

        private void AsyncStop(int AProcessID, IServerSession ASession)
        {
            IServerProcess LProcess = ASession.StartProcess(new ProcessInfo(FSession.SessionInfo));
            try
            {
                LProcess.Execute("StopProcess(" + AProcessID + ")", null);
            }
            finally
            {
                ASession.StopProcess(LProcess);
            }
        }

        private void ExecuteAsync()
        {
            try
            {
                ErrorList LErrors = null;
                TimeSpan LElapsed = TimeSpan.Zero;
                

                try
                {
                    ScriptExecutionUtility.ExecuteScript
                        (
                        FProcess,
                        FScript,
                        ScriptExecuteOption.All,
                        out LErrors,
                        out LElapsed,
                        (AStatistics, AResults) => Session.SafelyInvoke(new ReportScriptProgressHandler(AsyncProgress),
                                                                        new object[] {AStatistics, AResults})
                        );
                }
                finally
                {
                    Session.SafelyInvoke(new ExecuteFinishedHandler(AsyncFinish), new object[] {LErrors, LElapsed});
                }
            }
            catch(Exception LException)
            {
                SRFLogger.WriteLine(TraceLevel.Error, "Exception at ExecuteAsync {0}", LException);
                // Don't allow exceptions to go unhandled... the framework will abort the application
            }
        }

        private void AsyncProgress(PlanStatistics AStatistics, string AResults)
        {
            // Return what results we got even if stopped.
            FExecuteProgress(AStatistics, AResults);
        }

        private void AsyncFinish(ErrorList AErrors, TimeSpan AElapsedTime)
        {
            CleanupProcess();
            if (FIsRunning)
                FExecuteFinished(AErrors, AElapsedTime);
        }

        #region Nested type: AsyncStopHandler

        private delegate void AsyncStopHandler(int AProcessID, IServerSession ASession);

        #endregion
    }
{'fraction_non_alphanumeric': 0.06324716460611014, 'fraction_numerical': 0.0016348217022580974, 'mean_word_length': 1.3972569189321578, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5519089', 'n_tokens_mistral': 2316, 'n_tokens_neox': 2195, 'n_words': 544}