c# Как можно переписать код?
Есть код, обрабатывающий входной текст в режиме онлайн:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using TaskPromisDll.Args;
using TaskPromisDll.Enums;
namespace TaskPromisDll
{
public class Executor
{
public static event EventHandler<TextProccessedArgs> TextProccessed;
delegate ErrCode Method(string text, out string result);
static Dictionary<ActionType, Method> actions = new Dictionary<ActionType, Method>();
private static readonly object incomeSyncObj = new object();
private static readonly Queue<string> income = new Queue<string>();
private static Thread incomeThread;
private static readonly object resultsSyncObj = new object();
private static readonly Queue<Tuple<string, string>> results = new Queue<Tuple<string, string>>();
private static Thread resultsThread;
public static void Init()
{
actions.Add(ActionType.CountRepeatWordsInText, MethodsBox.CountRepeatWordsInText);
actions.Add(ActionType.FindAlphabet, MethodsBox.FindAlphabet);
actions.Add(ActionType.InverseString, MethodsBox.InverseString);
incomeThread = new Thread(() => {
while (true)
{
Random rnd = new Random();
int Method = rnd.Next(0, 3);
string text = String.Empty;
lock (incomeSyncObj)
if (income.Count > 0) text = income.Dequeue();
if (!String.IsNullOrWhiteSpace(text))
{
actions[(ActionType)Method](text,out var result);
lock (resultsSyncObj)
results.Enqueue(new Tuple<string, string>(text, result));
}
Thread.Sleep(100);
}
});
incomeThread.Start();
resultsThread = new Thread(() => {
while (true)
{
lock (resultsSyncObj)
if (results.Count > 0)
{
var result = results.Dequeue();
TextProccessed?.Invoke(null, new TextProccessedArgs { Result = result });
}
Thread.Sleep(100);
}
});
resultsThread.Start();
}
public static void ProccessString(string text)
{
lock (incomeSyncObj) income.Enqueue(text);
}
}
}
Как его можно написать более профессионально?