<commit_before>﻿using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;

namespace SharedLibraries
{
    public class Pipe
    {
        // Private
        private NamedPipeClientStream client = null;
        private NamedPipeServerStream server = null;
        private StreamReader streamRead;
        private StreamWriter streamWrite;
        private Thread readWriteThread;
        private bool active;

        // Public
        public bool isClient { get; }
        public bool isStarted { get { return active; } }
        public string name { get; }
        public string path { get; }

        // Accessors
        public StreamReader read { get { return streamRead; } }
        public StreamWriter write { get { return streamWrite; } }
        public Thread thread { get { return readWriteThread; } }

        // Constructors
        public Pipe(string name, bool isClient, string path)
        {
            this.isClient = isClient;
            this.path = path;
            this.name = name;
            active = false;
        }
        public Pipe(string name, bool isClient)
        {
            this.isClient = isClient;
            this.name = name;
            active = false;
        }

        // Public Methods
        public void start()
        {
            if (!active)
            {
                if (!isClient)
                {
                    startServer();
                }
                else
                {
                    startClient();
                }
                active = true;
            }
        }
        public void stop()
        {
            if (active)
            {
                try
                {
                    if (!isClient) { server.Close(); server = null; }
                    else { client.Close(); client = null; }
                }
                catch (NullReferenceException) { }
                Console.WriteLine("Disconnected: " + name);
                active = false;
            }
        }
        public void sendcommand(string command)
        {
            this.write.WriteLine(command);
            Console.WriteLine("sent: " + command);
        }
        public void startThread(ThreadStart start)
        {
            readWriteThread = new Thread(start);
        }
        public void stopThread(ThreadStart thread)
        {
            readWriteThread.Abort();
        }

        // Public Static Methods
        public static void disconnectPipes(Pipe[] pipelist)
        {
            System.Threading.Tasks.Parallel.ForEach(pipelist, pipe => {
                pipe.stop();
            });
        }
        public static void disconnectPipes(System.Collections.Generic.List<Pipe> pipelist)<commit_msg>Added ability to check if thread is active<commit_after>﻿using System;
using System.IO;
using System.IO.Pipes;
using System.Threading;

namespace SharedLibraries
{
    public class Pipe
    {
        // Private
        private NamedPipeClientStream client = null;
        private NamedPipeServerStream server = null;
        private StreamReader streamRead;
        private StreamWriter streamWrite;
        private Thread readWriteThread;
        private bool streamActive;
        private bool threadActive;

        // Public
        public bool isClient { get; }
        public bool streamStarted { get { return streamActive; } }
        public bool threadStarted { get { return threadActive; } }
        public string name { get; }
        public string path { get; }

        // Accessors
        public StreamReader read { get { return streamRead; } }
        public StreamWriter write { get { return streamWrite; } }
        public Thread thread { get { return readWriteThread; } }

        // Constructors
        public Pipe(string name, bool isClient, string path)
        {
            this.isClient = isClient;
            this.path = path;
            this.name = name;
            streamActive = false;
            threadActive = false;
        }
        public Pipe(string name, bool isClient)
        {
            this.isClient = isClient;
            this.name = name;
            streamActive = false;
            threadActive = false;
        }

        // Public Methods
        public void start()
        {
            if (!streamActive)
            {
                if (!isClient)
                {
                    startServer();
                }
                else
                {
                    startClient();
                }
                streamActive = true;
            }
        }
        public void stop()
        {
            if (streamActive)
            {
                try
                {
                    if (!isClient) { server.Close(); server = null; }
                    else { client.Close(); client = null; }
                }
                catch (NullReferenceException) { }
                Console.WriteLine("Disconnected: " + name);
                streamActive = false;
            }
        }
        public void sendcommand(string command)
        {
            write.WriteLine(command);
            Console.WriteLine("sent: " + command);
        }
        public void startThread(ThreadStart start)
        {
            readWriteThread = new Thread(start);
            threadActive = true;
        }
        public void stopThread(ThreadStart thread)
        {
            readWriteThread.Abort();
            threadActive = false;
        }

        // Public Static Methods
        public static void disconnectPipes(Pipe[] pipelist)
        {
            System.Threading.Tasks.Parallel.ForEach(pipelist, pipe => {
                pipe.stop();
            });
        }
        public static void disconnectPipes(System.Collections.Generic.List<Pipe> pipelist)
{'fraction_non_alphanumeric': 0.07913419913419914, 'fraction_numerical': 0.0, 'mean_word_length': 1.3422546634225467, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7450687', 'n_tokens_mistral': 1407, 'n_tokens_neox': 1345, 'n_words': 361}