public void Test() { var CustomThreadPool = new CustomThreadPool(2); var Results0 = new List <int>(); var Results1 = new List <int>(); var CountdownEvent = new CountdownEvent(2); CustomThreadPool.AddTask(0, () => { Thread.Sleep(10); Results0.Add(0); }); CustomThreadPool.AddTask(0, () => { Results0.Add(1); CountdownEvent.Signal(); }); CustomThreadPool.AddTask(1, () => { Results1.Add(0); CountdownEvent.Signal(); }); CountdownEvent.Wait(); Thread.Sleep(10); Assert.True(CustomThreadPool.GetLoopIterCount(0) <= 2); Assert.True(CustomThreadPool.GetLoopIterCount(1) <= 2); Assert.Equal("0,1", Results0.ToStringArray()); Assert.Equal("0", Results1.ToStringArray()); CustomThreadPool.Stop(); }
public MainController(Network network, TumblerRuntime runtime, CustomThreadPool threadPool) { _Network = network; _Runtime = runtime ?? throw new ArgumentNullException(nameof(runtime)); _Repository = new ClassicTumblerRepository(_Network, _Runtime); _ThreadPool = threadPool; }
private void SecureUpToItem(int maxItem) { maxItem = Math.Min(maxItem, Length); var itemsToRead = maxItem - BufferedItemsCount; //Console.WriteLine("SecureUpToItem: {0}, {1}, {2}", MaxItem, BufferedItemsCount, ItemsToRead); if (itemsToRead > 0) { try { var dataLength = itemsToRead * StructSize; var data = new byte[dataLength]; /* * Stream.BeginRead(Data, 0, DataLength, (AsyncState) => * { * int Readed = Stream.EndRead(AsyncState); * CachedValues.AddRange(PointerUtils.ByteArrayToArray<TType>(Data)); * }, null); */ CustomThreadPool.AddTask(0, () => { var readed = _stream.Read(data, 0, dataLength); _cachedValues.AddRange(PointerUtils.ByteArrayToArray <TType>(data)); }); } catch (Exception exception) { Console.Error.WriteLine(exception); } } }
public InvoiceRepository(ApplicationDbContextFactory contextFactory, string dbreezePath, Network network) { _Engine = new DBreezeEngine(dbreezePath); _IndexerThread = new CustomThreadPool(1, "Invoice Indexer"); _Network = network; _ContextFactory = contextFactory; }
public virtual void Generate(int worldSeed) { int chunkSeed = Generator.GetPerIslandSeed(this); creatingChunkStopwatch = System.Diagnostics.Stopwatch.StartNew(); random = new Random(chunkSeed); elevations = new List <float>(); islandColors = new List <Color>(); CreatingPoolTask = delegate { PoissonDiscSampler sampler = new PoissonDiscSampler(Size, Size, minPointRadius); Polygon polygon = new Polygon(); //Add uniformly-spaced points foreach (Vector2 sample in sampler.Samples(chunkSeed)) { polygon.Add(new Vertex((double)sample.x, (double)sample.y)); } //add points at corners so chunk will be always square shaped polygon.Add(new Vertex(0, 0)); polygon.Add(new Vertex(0, Size)); polygon.Add(new Vertex(Size, 0)); polygon.Add(new Vertex(Size, Size)); //Add some randomly sampled points for (int i = 0; i < randomPoints - 4; i++) { polygon.Add(new Vertex(random.Range(0.0f, Size), random.Range(0.0f, Size))); } TriangleNet.Meshing.ConstraintOptions options = new TriangleNet.Meshing.ConstraintOptions() { ConformingDelaunay = true }; mesh = (TriangleNet.Mesh)polygon.Triangulate(options); // Sample perlin noise to get elevations foreach (Vertex vert in mesh.Vertices) { float height = Generator.GetTerrainHeight((float)vert.x, (float)vert.y, this); Color color = Generator.GetTerrainColor((float)vert.x, (float)vert.y, height, this); elevations.Add(height); islandColors.Add(color); } CreateMeshRequest = true; //let this be always the last piece of code here try { bin = new TriangleBin(mesh, Size, Size, minPointRadius * 2.0f); } catch (Exception e) { Debug.Log("triangulation failed!"); } }; CustomThreadPool.AddTask(CreatingPoolTask); }
public MainWindow() { InitializeComponent(); _pool = CustomThreadPool.GetInstance(MinThreadCount, MaxThreadCount); _accumulator = new ConcurrentAccumulator(1000, 6000, OnFlush); _accumulator.OnObjectCountChange += UpdateObjectCount; LThreads.Content = $"Thread count: {_handles.Count}"; }
private void ItemCompleted(CustomThreadPool pool, ThreadPoolWorkItem workitem) { var item = workitem as DownloadItem; if (item == null) return; InvokeFileDownloaded(item.Uri); }
private void OnWindowClosing(object sender, CancelEventArgs e) { foreach (var t in _handles) { CustomThreadPool.CancelUserTask(t); } _pool.Dispose(); _accumulator.Dispose(); }
private void CopyFolder(string sourcePath, string destinationPath) { var dir = new DirectoryInfo(sourcePath); foreach (var directory in dir.GetDirectories()) { var newDir = new DirectoryInfo(Path.Combine(destinationPath, directory.Name)); Dispatcher.Invoke(() => LbLog.Items.Add("Create " + newDir.FullName)); newDir.Create(); CopyFolder(directory.FullName, newDir.FullName); } foreach (var file in dir.GetFiles()) { _countTotal++; Dispatcher.Invoke(() => PbProgress.Maximum = _countTotal); try { var pool = CustomThreadPool.GetInstance(); var destination = Path.Combine(destinationPath, file.Name); pool.QueueUserTask( () => { file.CopyTo(destination, true); return(true); }, ts => { lock (this) { if (ts.Success) { _countCopied++; } var message = file.FullName + " -> " + destination; Dispatcher.Invoke(() => { LbLog.Items.Add(ts.Pid.ToString().PadRight(5) + (ts.Success ? "Success: " : "Fail: ").PadRight("Success: ".Length) + message + (ts.Success ? "" : " : " + ts.InnerException.Message)); LbLog.Items.MoveCurrentToLast(); LbLog.ScrollIntoView(LbLog.Items.CurrentItem); PbProgress.Value = _countCopied; LProgress.Content = $"{_countCopied}/{_countTotal}"; }); } }); } catch (Exception ex) { LbLog.Items.Add(ex.Message); } } }
/// <summary> /// Registers a dispatcher. NOTE: If you register a dispatcher, please make sure you call Close/Dispose at the end of your application /// </summary> /// <param name="dispatcher"></param> public void RegisterDispatcher(IDispatcher dispatcher) { _dispatchers.Add(dispatcher); if (_threadPool == null) { _threadPool = new CustomThreadPool(this, 10); _threadPool.Start(); } }
public static void Dispose() { if (modelThreadPool == null) { return; } modelThreadPool.Dispose(); modelThreadPool = null; }
private void SaveThreadsCount_Click(object sender, RoutedEventArgs e) { if (!IudThreadCount.Value.HasValue) { return; } BSave.IsEnabled = false; IudThreadCount.IsReadOnly = true; _pool = CustomThreadPool.GetInstance(IudThreadCount.Value.Value, IudThreadCount.Value.Value); }
public Writer(Index index) { this.index = index; pool = new MiscUtil.Threading.CustomThreadPool(index.Path); pool.SetMinMaxThreads(0, 5); pool.IdlePeriod = 10; pool.WorkerThreadPriority = ThreadPriority.BelowNormal; pool.WorkerThreadsAreBackground = false; pool.BeforeWorkItem += new BeforeWorkItemHandler(pool_BeforeWorkItem); pool.AfterWorkItem += new AfterWorkItemHandler(pool_AfterWorkItem); pool.WorkerException += new ThreadPoolExceptionHandler(pool_WorkerException); }
private void BRemove_Click(object sender, RoutedEventArgs e) { if (_handles.Count == 0) { return; } var thread = _handles.First(); CustomThreadPool.CancelUserTask(thread); _handles.Remove(thread); LThreads.Content = $"Thread count: {_handles.Count}"; }
public MultiThreadEvaluationPopulation(int size, T ancestor, IFitnessFunction fitnessFunction, ISelectionMethod selectionMethod, CustomThreadPool pool, bool allowDuplicateChromosomes) : base(size, fitnessFunction, selectionMethod) { TasksPool = pool; if (ancestor != null && size > 0) { AddChromosome(ancestor); } TasksPool.waitAllTasks(); // the first evaluation can't be parallel, due to gui input problems for (int s = 1; s < size; ++s) { AddChromosome(this[0].CreateNew()); } allowDupes = allowDuplicateChromosomes; }
public InvoiceRepository(ApplicationDbContextFactory contextFactory, string dbreezePath) { int retryCount = 0; retry: try { _Engine = new DBreezeEngine(dbreezePath); } catch when(retryCount++ < 5) { goto retry; } _IndexerThread = new CustomThreadPool(1, "Invoice Indexer"); _ContextFactory = contextFactory; }
public InvoiceRepository(ApplicationDbContextFactory contextFactory, string dbreezePath, BTCPayNetworkProvider networks) { int retryCount = 0; retry: try { _Engine = new DBriizeEngine(dbreezePath); } catch when(retryCount++ < 5) { goto retry; } _IndexerThread = new CustomThreadPool(1, "Invoice Indexer"); _ContextFactory = contextFactory; _Networks = networks.UnfilteredNetworks; }
/// <summary> /// creates a new population from seralization (returned from serializePopulation()) /// ancesstor is set to the first of the deserialized chromosomes /// </summary> /// <param name="popSerialization"></param> public static MultiThreadEvaluationPopulation <T> desrializePopulation( List <string> popSerialization, ChromosomeGenerator deserializer, IFitnessFunction fitnessFunction, ISelectionMethod selectionMethod, CustomThreadPool pool, bool allowDuplicateChromosomes) { IChromosome ancestor = deserializer(popSerialization[0]); MultiThreadEvaluationPopulation <T> newpop = new MultiThreadEvaluationPopulation <T>(0, (T)ancestor, fitnessFunction, selectionMethod, pool, allowDuplicateChromosomes); newpop.size = popSerialization.Count; for (int cc = 0; cc < popSerialization.Count; ++cc) { newpop.AddChromosome(deserializer(popSerialization[cc])); } return(newpop); }
public EngineAccessor(string directory) { this.directory = directory; try { _Pool = new CustomThreadPool(1, "Repository"); RenewEngine(); } catch { Dispose(); throw; } _Renew = new Timer((o) => { try { RenewEngine(); } catch { } }); _Renew.Change(0, (int)TimeSpan.FromSeconds(60).TotalMilliseconds); }
private void StopClick(object sender, MouseButtonEventArgs e) { CustomThreadPool.Suspend = true; for (var i = _complete / Step + 1; i < _currentTask.Count; i++) { if (_currentTask[i].State != TaskState.NotStarted) { continue; } CustomThreadPool.CancelUserTask(_currentTask[i]); } CustomThreadPool.Suspend = false; var file = new FileInfo(LDestination.Content.ToString()); lock (_destFile) { file.Delete(); } PbProgress.Value = PbProgress.Maximum; }
public override void DownloadAll(IEnumerable<Uri> uris, DirectoryInfo output) { var poolName = string.Format("downloads to {0}", output.Name); var pool = new CustomThreadPool(poolName) { MinThreads = 1, MaxThreads = 4 }; pool.AfterWorkItem += ItemCompleted; foreach (Uri uri in uris) { var workItem = new DownloadItem(DownloadOne, uri, output); pool.AddWorkItem(workItem); } while(pool.WorkingThreads > 0) {} InvokeFinished(); }
public ThreadPoolWrapper() { ThreadPool = new CustomThreadPool(30, "MainController Processor"); }
private void QueueHandler() { //this will receive a notification when there is a new HttpRequest in the thread. workerThreadPool = new CustomThreadPool(); int minThreads = 1; int maxThreads = 20; int processedQueueItemsCount = 0; workerThreadPool.SetMinMaxThreads(minThreads, maxThreads); logger.Info("[" + MethodBase.GetCurrentMethod().Name + "()] - HTTP Web Request thread started"); while (1 == 1) { resourceLockOut.WaitOne(); while (itemList.Count > 0) { if ((itemList.Count > 100) && (itemList.Count % 1000 == 0)) { logger.Error("[" + MethodBase.GetCurrentMethod().Name + "()] - http queue size: " + itemList.Count); } lock (itemList) { try { //Fetch item from front of queue and request a thread from thread pool to process it HttpWebRequest req = itemList.Dequeue(); processHTTPWebRequestImmediately(req); processedQueueItemsCount++; } catch (Exception e) { logger.Error("[" + MethodBase.GetCurrentMethod().Name + "()] - System Exception doing QUEUE Request: " + e.ToString()); } } } } }
private void Window_Closing(object sender, CancelEventArgs e) { CustomThreadPool.GetInstance().Dispose(); }
public void TestStop() { var CustomThreadPool = new CustomThreadPool(2); CustomThreadPool.Stop(); }
private void QueueHandler() { logger.Debug(MethodBase.GetCurrentMethod().Name + "() - START"); //this will receive a notification when there is a new HttpRequest in the thread. workerThreadPool = new CustomThreadPool(); int minThreads = 1; int maxThreads = ConnectionConfig.QueueHelperMaxThreads_OutgoingMessage; workerThreadPool.SetMinMaxThreads(minThreads, maxThreads); logger.Info(MethodBase.GetCurrentMethod().Name + "() - queue handler thread started"); while (true) { resourceLockOut.WaitOne(); while (itemList.Count > 0 && !ConnectionManager.Instance.IsReconnecting) { if ((itemList.Count > 100) && (itemList.Count % 1000 == 0)) { logger.Info(MethodBase.GetCurrentMethod().Name + "() - queue size: " + itemList.Count); } IMessageToSend item = null; lock (itemList) { try { //Fetch item from front of queue and request a thread from thread pool to process it item = itemList.Dequeue(); } catch (Exception e) { logger.Error(MethodBase.GetCurrentMethod().Name + "() - System Exception doing QUEUE Request: " + e.ToString()); } } if (item != null) addRequestToWorkerPool(item); } } logger.Debug(MethodBase.GetCurrentMethod().Name + "() - END"); }
public ServerManager(int NumberOfThreads = 1) { this.CustomThreadPool = new CustomThreadPool(NumberOfThreads); }
//Public method exposed for starting the thread pools private void initThreadPool() { logger.Debug(MethodBase.GetCurrentMethod().Name + "() - START"); Console.Write(DateTime.Now.ToString() + " Creating Thread Pool: ThreadPool_SendRESTMessage... "); //Create the thread pool workerThreadPool = new CustomThreadPool(); int minThreads = 1; int maxThreads = ConnectionConfig.QueueHelperMaxThreads_OutgoingRESTMessage; workerThreadPool.SetMinMaxThreads(minThreads, maxThreads); Console.WriteLine(" OK"); logger.Debug(MethodBase.GetCurrentMethod().Name + "() - END"); }
public EngineAccessor(string directory) { this.directory = directory; _Pool = new CustomThreadPool(1, "Repository"); RenewEngine(); }
private void QueueHandler() { //this will receive a notification when there is a new HttpRequest in the thread. workerThreadPool = new CustomThreadPool(); int minThreads = 1; int maxThreads = 20; workerThreadPool.SetMinMaxThreads(minThreads, maxThreads); logger.Info("[" + MethodBase.GetCurrentMethod().Name + "()] - HTTP Web Request thread started"); while (1 == 1) { resourceLockOut.WaitOne(); while (itemList.Count > 0) { if ((itemList.Count > 100) && (itemList.Count % 1000 == 0)) { logger.Error("[" + MethodBase.GetCurrentMethod().Name + "()] - http queue size: " + itemList.Count); if (itemList.Count > 5000) { //we're in trouble if we get to this point //better to dump queue and start over? itemList.Clear(); logger.Fatal("[" + MethodBase.GetCurrentMethod().Name + "()] - queue size exceeds max threshold: " + itemList.Count + ", clearing queue!"); } } lock (itemList) { try { //Fetch item from front of queue and request a thread from thread pool to process it HttpWebRequest req = itemList.Dequeue(); addHTTPRequestDelegateToThreadPool(req); } catch (Exception e) { logger.Error("[" + MethodBase.GetCurrentMethod().Name + "()] - System Exception doing QUEUE Request: " + e.ToString()); } } } } }
void pool_WorkerException(CustomThreadPool pool, ThreadPoolWorkItem workItem, Exception e, ref bool handled) { var mine = workItem.Parameters[0] as IndexWorkItem; if (mine != null) { mine.FireError(e, ref handled); } }
void pool_BeforeWorkItem(CustomThreadPool pool, ThreadPoolWorkItem workItem, ref bool cancel) { var mine = workItem.Parameters[0] as IndexWorkItem; if (mine != null) { mine.FireBeforeRun(); } }
void pool_AfterWorkItem(CustomThreadPool pool, ThreadPoolWorkItem workItem) { var mine = workItem.Parameters[0] as IndexWorkItem; if (mine != null) { mine.FireComplete(); } }
/// <summary> /// /// </summary> /// <param name="numberOfItemsToBuffer"></param> /// <param name="stream"></param> public StreamStructCachedArrayWrapper(int numberOfItemsToBuffer, Stream stream) { _numberOfItemsToBuffer = numberOfItemsToBuffer; _stream = stream; CustomThreadPool = new CustomThreadPool(1); }
private void QueueHandler() { //this will receive a notification when there is a new HttpRequest in the thread. workerThreadPool = new CustomThreadPool(); int minThreads = 1; int maxThreads = 1; int processedQueueItemsCount = 0; workerThreadPool.SetMinMaxThreads(minThreads, maxThreads); logger.Info("[" + MethodBase.GetCurrentMethod().Name + "()] - HTTP Web Request thread started"); while (1 == 1) { resourceLockOut.WaitOne(); while (itemList.Count > 0) { if ((itemList.Count > 100) && (itemList.Count % 1000 == 0)) { logger.Error("[" + MethodBase.GetCurrentMethod().Name + "()] - http queue size: " + itemList.Count); } lock (itemList) { try { //Fetch item from front of queue and request a thread from thread pool to process it HttpWebRequest req = itemList.Dequeue(); using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { using (StreamReader streamIn = new StreamReader(responseStream)) { String strResponse = streamIn.ReadToEnd(); streamIn.Close(); } responseStream.Flush(); responseStream.Close(); } response.Close(); } processedQueueItemsCount++; } catch (Exception e) { logger.Error("[" + MethodBase.GetCurrentMethod().Name + "()] - System Exception doing QUEUE Request: " + e.ToString()); } } } } }