public AutoReadSupporter(ISMemCtrler <T> _smemCtrler) { smemCtrler = _smemCtrler; task = new MyTask( #if !(NET35 || NET20) async #endif (_) => { if (smemCtrler is null) { return; } while (!IsRunning && !disposingValue && !disposedValue) { smemCtrler.Read(); #if !(NET35 || NET20) await #endif MyTask.Delay((int)Interval.TotalMilliseconds); } }); }
public void ContinueWithShutdownTest(int threadCount) { var pool = new MyThreadPool(threadCount); var tasks = new IMyTask <int> [threadCount + 10]; var resetEvent = new ManualResetEvent(false); tasks[0] = pool.QueueTask(() => { resetEvent.WaitOne(); return(1); }); for (var i = 1; i < tasks.Length; ++i) { tasks[i] = tasks[i - 1].ContinueWith((j) => { return(++j); }); } pool.Shutdown(); resetEvent.Set(); for (var i = 1; i < tasks.Length; ++i) { Assert.Throws <ThreadPoolShutdownException>(() => _ = tasks[i].Result); Assert.IsFalse(tasks[i].IsCompleted); } while (pool.ActiveThreadCount != 0) { ; } }
public void ContinueWithTest([Values(1, 3, 5, 10)] int threadCount, [Values(5, 10, 20)] int taskCount) { var pool = new MyThreadPool(threadCount); var tasks = new IMyTask <int> [taskCount]; var counters = new int[taskCount]; var resetEvent = new ManualResetEvent(false); tasks[0] = pool.QueueTask(() => { resetEvent.WaitOne(); Interlocked.Increment(ref counters[0]); return(1); }); for (var i = 1; i < taskCount; ++i) { var localIndex = i; tasks[i] = tasks[i - 1].ContinueWith((j) => { resetEvent.WaitOne(); Interlocked.Increment(ref counters[localIndex]); return(j * 2); }); } resetEvent.Set(); for (var i = 0; i < taskCount; ++i) { Assert.AreEqual(tasks[i].Result, Math.Pow(2, i)); Assert.AreEqual(1, counters[i]); } }
public void CanSolveManyTaskFastBy75ThreadsAndFinellyGetAllResult() { var numberOfTasksSoMuch = 500; threadPool = new MyThreadPool.ThreadPool(75); var tasks = new IMyTask <int> [numberOfTasksSoMuch]; for (int i = 0; i < tasks.Length - 10; i++) { tasks[i] = threadPool.AddTask(() => { Thread.Sleep(500); return(5); }); } for (int i = 0; i < 10; i++) { tasks[numberOfTasksSoMuch - 10 + i] = tasks[i].ContinueWith(x => x + x); } int result = 0; for (int i = 0; i < tasks.Length; i++) { result += tasks[i].Result; } Assert.AreEqual(2550, result); }
public void Run() { while (!isDisposed) { IMyTask task = data[Thread.CurrentThread].Dequeue(); if (task == null) { int indexStolenThread = random.Next(0, data.Count); Thread stolenThread = data.ElementAt(indexStolenThread).Key; if (!data.ElementAt(indexStolenThread).Value.IsEmpty()) { IMyTask stolenTask = data.ElementAt(indexStolenThread).Value.Dequeue(); if (stolenTask != null) { data[Thread.CurrentThread].Enqueue(stolenTask); Thread.Sleep(10); } } } else { task.Execute(); } } }
public void PoolSizeTest() { var stopWatch = new Stopwatch(); var tasks = new IMyTask <int> [POOL_SIZE]; var handler = new ManualResetEvent(true); var count = 0; stopWatch.Start(); for (int i = 0; i < POOL_SIZE; i++) { var localI = i; tasks[localI] = pool.Enqueue(() => { count++; handler.WaitOne(); handler.Reset(); return(localI); }); } for (int i = 0; i < POOL_SIZE; i++) { if (i != tasks[i].Result) { Assert.Fail("Values didn't match"); } } handler.Close(); Assert.AreEqual(POOL_SIZE, count); }
public void Enqueue <TResult>(IMyTask <TResult> a) { if (data.Count != 0) { int indexToAddTask = random.Next(0, data.Count); data.Values.ToList()[indexToAddTask].Enqueue(a); } }
public void Enqueue(IMyTask task) { mutex.WaitOne(); tasks.Add(task); mutex.ReleaseMutex(); }
public void Enqueue <TResult>(IMyTask <TResult> task) { CheckDisposed(); if (task == null) { throw new ArgumentNullException(nameof(task), "Illegal null task"); } _queue.Add(task.Execute); }
public void TestWithException() { var tasks = new IMyTask <int> [100]; for (int i = 0; i < 100; i++) { int tempI = i; tasks[i] = myThreadPool.AddTask(() => tempI / (tempI - tempI)); } var temp = tasks[0].Result; }
public void CountOfThreadTest() { var tasks = new IMyTask <string> [100]; for (int i = 0; i < 100; i++) { int temp = i; tasks[i] = myThreadPool.AddTask(() => $"It is {temp++}"); } Assert.AreEqual(10, myThreadPool.CountOfAliveThreads()); }
public void SimpleThreadTest() { var tasks = new IMyTask <string> [100]; for (int i = 0; i < 100; i++) { int temp = i; tasks[i] = myThreadPool.AddTask(() => $"It is {temp++}"); } for (int i = 0; i < 100; i++) { Assert.AreEqual(tasks[i].Result, $"It is {i++}"); } }
public void MultipleTasksWorkTest() { var tasks = new IMyTask <string> [5]; for (int i = 0; i < 5; ++i) { tasks[i] = testPool.AddTask(() => "opa"); } foreach (var task in tasks) { Assert.AreEqual("opa", task.Result); } }
// else { MessageBox.Show("Дата таску не нашла"); // return null; public List <IMyTask> ReadAllTasks() { string[] allFoundFiles = Directory.GetFiles(@"../DataJson/", @"*.json", SearchOption.AllDirectories); List <IMyTask> tasks = new List <IMyTask>(); foreach (string path in allFoundFiles) { IMyTask tsk = DeserializeTask(path); if (tsk != null) { tasks.Add(tsk); } } return(tasks); }
public void CheckItCanSolveTasks() { var threadAmount = 10; var myThreadPool = new MyThreadPool(threadAmount); var taskAmount = 20; var tasks = new IMyTask <int> [taskAmount]; for (var i = 0; i < taskAmount; i++) { tasks[i] = myThreadPool.AddTask(new Func <int>(() => 2 * 21)); Assert.AreEqual(42, tasks[i].Result); } myThreadPool.Shutdown(); }
public void WaitingTasksTest() { const int numberOfTasks = 10; var tasks = new IMyTask <int> [numberOfTasks]; for (int i = 0; i < numberOfTasks; ++i) { var localI = i; tasks[i] = threadPool.Submit(() => localI); } for (int i = 0; i < numberOfTasks; ++i) { Assert.AreEqual(i, tasks[i].Result); } }
public void ShutdownedThreadPoolExecutesAlreadyTakenTasksTest() { const int numberOfTasks = 10; var tasks = new IMyTask <int> [numberOfTasks]; for (int i = 0; i < numberOfTasks; ++i) { var localI = i; tasks[i] = threadPool.Submit(() => localI * localI); } threadPool.Shutdown(); for (int i = 0; i < numberOfTasks; ++i) { Assert.AreEqual(i * i, tasks[i].Result); } }
public void EnqueueFunctionalityTest() { int size = 10; var tasks = new IMyTask <int> [10]; for (int i = 0; i < size; i++) { var localI = i; tasks[localI] = pool.Enqueue(() => 1000 * (localI + 1)); } for (int i = 0; i < size; i++) { Assert.AreEqual((i + 1) * 1000, tasks[i].Result); } }
public void TryAddTaskToShutThreadPool() { var threadAmount = 10; var myThreadPool = new MyThreadPool(threadAmount); myThreadPool.Shutdown(); var taskAmount = 2000; var tasks = new IMyTask <int> [taskAmount]; for (var i = 0; i < taskAmount; i++) { tasks[i] = myThreadPool.AddTask(() => 7 * 8); Assert.AreEqual(56, tasks[i].Result); } }
public void ContinueWithFunctionalityTest() { int size = 10; var tasks = new IMyTask <string> [10]; for (int i = 0; i < size; i++) { var localI = i; tasks[localI] = pool.Enqueue(() => (localI + 1) * 10) .ContinueWith(x => x.ToString()); } for (int i = 0; i < size; i++) { Assert.AreEqual(((i + 1) * 10).ToString(), tasks[i].Result); } }
public void ShutdowntTest() { var tasks = new IMyTask <int> [100]; for (int i = 0; i < 100; i++) { tasks[i] = myThreadPool.AddTask(() => 100); } myThreadPool.Shutdown(); var tempTask = myThreadPool.AddTask(() => "Ololo"); Assert.IsNull(tempTask); for (int i = 0; i < 100; i++) { Assert.IsTrue(tasks[i].IsCompleted); Assert.AreEqual(100, tasks[i].Result); } }
public void NumberOfTaskMoreThatThreadsAndTheyWillCompleteAndGetResult() { var numberTasks = 5; var tasks = new IMyTask <int> [10]; threadPool = new MyThreadPool.ThreadPool(4); for (int i = 0; i < numberTasks; i++) { tasks[i] = threadPool.AddTask(() => { Thread.Sleep(1500); return(5); }); } for (int i = 0; i < numberTasks; i++) { Assert.AreEqual(5, tasks[i].Result); } }
public void ConcurrentShutdownAndEnqueueTest() { var mre = new ManualResetEvent(false); var tasks = new IMyTask <int> [POOL_SIZE]; var threads = new Thread[POOL_SIZE]; var exceptions = new List <Exception>(); for (int i = 0; i < POOL_SIZE; i++) { var local = i; threads[local] = new Thread(() => { try { tasks[local] = pool.Enqueue(() => { mre.WaitOne(); return(100); }); } catch (Exception e) { exceptions.Add(e); } }); threads[local].Start(); } pool.Shutdown(); Thread.Sleep(1000); mre.Close(); foreach (var e in exceptions) { Assert.AreEqual("Pool is stopped", e.Message); } }
public void MyThreadPoolShouldSolveTasks() { const int numOfThreads = 4; const int numOfTasks = 16; var threadPool = new MyThreadPool(numOfThreads); var tasks = new IMyTask <int> [numOfTasks]; int variable = 2; int trueResult = variable * variable; for (int i = 0; i < numOfTasks; i++) { tasks[i] = threadPool.AddTask(() => variable * variable); } for (int i = 0; i < numOfTasks; i++) { Assert.AreEqual(trueResult, tasks[i].Result); Assert.IsTrue(tasks[i].IsCompleted); } }
public void CanSolveTasksByOneThread() { threadPool = new MyThreadPool.ThreadPool(1); var tasks = new IMyTask <int> [10]; for (int i = 0; i < 10; i++) { tasks[i] = threadPool.AddTask(() => { Thread.Sleep(250); return(5); }); } var result = 0; for (int i = 0; i < 10; i++) { result += tasks[i].Result; } Assert.AreEqual(50, result); }
public void ManyContinueWithTest() { var waitForSubmitAllTasks = new CountdownEvent(1); var task = threadPool.Submit(() => { waitForSubmitAllTasks.Wait(); return(true); }); const int numberOfTasks = 30; var tasks = new IMyTask <bool> [numberOfTasks]; tasks[0] = task.ContinueWith(x => x || true); for (int i = 1; i < numberOfTasks; ++i) { tasks[i] = tasks[i - 1].ContinueWith(x => x || true); } waitForSubmitAllTasks.Signal(); for (int i = 0; i < numberOfTasks; ++i) { Assert.AreEqual(true, tasks[i].Result); } }
public void Unsubscribe(IMyTask task) { _subscribers.Remove(task); }
public void SaveInJson(IMyTask task) { string json = JsonConvert.SerializeObject(task); File.WriteAllText(@"../DataJson/" + task.Name + ".json", json); }
public void SetLitener(IMyTask listener)//传递一个实现了IMyTask接口的对象来监听任务结果 { _myTask = listener; }
public void Subscribe(IMyTask task) { _subscribers.Add(task); }