<commit_before>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;

namespace EventHook.Helpers
{
    internal class SharedMessagePump
    {
        static Lazy<TaskScheduler> _scheduler;
        static Lazy<MessageHandler> _messageHandler;
  
        static SharedMessagePump()
        {
            _scheduler = new Lazy<TaskScheduler>(() =>
            {
                Dispatcher dispatcher = Dispatcher.FromThread(Thread.CurrentThread);
                if (dispatcher != null)
                {
                    if (SynchronizationContext.Current != null)
                        return TaskScheduler.FromCurrentSynchronizationContext();
                }           
            

                TaskScheduler current = null;

                //if current task scheduler is null, create a message pump 
                //http://stackoverflow.com/questions/2443867/message-pump-in-net-windows-service
                //use async for performance gain!
                new Task(() =>
                {
                    System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
                    {
                        current = TaskScheduler.FromCurrentSynchronizationContext();
                    }

               ), DispatcherPriority.Normal);
                    System.Windows.Threading.Dispatcher.Run();
                }).Start();

                while (current == null)
                {
                    Thread.Sleep(10);
                }

                return current;

            });

            _messageHandler = new Lazy<MessageHandler>(() =>
                {
                    MessageHandler msgHandler = null;

                    new Task((e) =>
                    {
                        msgHandler = new MessageHandler();
                    }, GetTaskScheduler()).Start();

                    while (msgHandler == null)
                    {
                        Thread.Sleep(10);
                    };

                    return msgHandler;
                });
        }

        internal static void Initialize()
        {
            GetTaskScheduler();
            GetHandle();
        }

        internal static TaskScheduler GetTaskScheduler()
        {
            return _scheduler.Value;
        }

        internal static IntPtr GetHandle()
        {
            var handle = IntPtr.Zero;

            try
            {
                handle = Process.GetCurrentProcess().MainWindowHandle;

                if (handle != IntPtr.Zero)
                    return handle;
            }
            catch { }

            return _messageHandler.Value.Handle;
        }

    }

    internal class MessageHandler : NativeWindow
    {<commit_msg>use flag to identify UI thread<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Threading;

namespace EventHook.Helpers
{
    internal class SharedMessagePump
    {
        private static bool _hasUIThread = false;

        static Lazy<TaskScheduler> _scheduler;
        static Lazy<MessageHandler> _messageHandler;

        static SharedMessagePump()
        {
            _scheduler = new Lazy<TaskScheduler>(() =>
            {
                Dispatcher dispatcher = Dispatcher.FromThread(Thread.CurrentThread);
                if (dispatcher != null)
                {
                    if (SynchronizationContext.Current != null)
                    {
                        _hasUIThread = true;
                        return TaskScheduler.FromCurrentSynchronizationContext();
                    }
                }           
            
                TaskScheduler current = null;

                //if current task scheduler is null, create a message pump 
                //http://stackoverflow.com/questions/2443867/message-pump-in-net-windows-service
                //use async for performance gain!
                new Task(() =>
                {
                    System.Windows.Threading.Dispatcher.CurrentDispatcher.BeginInvoke(new Action(() =>
                    {
                        current = TaskScheduler.FromCurrentSynchronizationContext();
                    }

               ), DispatcherPriority.Normal);
                    System.Windows.Threading.Dispatcher.Run();
                }).Start();

                while (current == null)
                {
                    Thread.Sleep(10);
                }

                return current;

            });

            _messageHandler = new Lazy<MessageHandler>(() =>
                {
                    MessageHandler msgHandler = null;

                    new Task((e) =>
                    {
                        msgHandler = new MessageHandler();
                    }, GetTaskScheduler()).Start();

                    while (msgHandler == null)
                    {
                        Thread.Sleep(10);
                    };

                    return msgHandler;
                });
        }

        internal static void Initialize()
        {
            GetTaskScheduler();
            GetHandle();
        }

        internal static TaskScheduler GetTaskScheduler()
        {
            return _scheduler.Value;
        }

        internal static IntPtr GetHandle()
        {
            var handle = IntPtr.Zero;

            if (_hasUIThread)
            {
                try
                {
                    handle = Process.GetCurrentProcess().MainWindowHandle;

                    if (handle != IntPtr.Zero)
                        return handle;
                }
                catch { }
            }

            return _messageHandler.Value.Handle;
        }

    }

    internal class MessageHandler : NativeWindow
    {
{'fraction_non_alphanumeric': 0.07964309319233312, 'fraction_numerical': 0.0036351619299405157, 'mean_word_length': 1.2164042475283778, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 21, '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': '3816387', 'n_tokens_mistral': 1439, 'n_tokens_neox': 1290, 'n_words': 276}