// Entry-point for processing messages public virtual void ProcessArrivalMessages() { try { // Getting all necessary info and initialization stores before processing var validationSettings = GetValidationSettingsForProducts(checkingProductIds); var validationRules = GetValidationRules(); SetRulesForSettings(validationSettings, validationRules); lock (locker) { messagesStore = new MessagesStore(); var messages = _arrivalMessagesRepository.GetNotProcessedMessages(checkingProductIds, null) .OrderBy(t => t.CreatedOn).ToList(); messagesStore.Messages = messages; InitDictionary(messages); } SmartThreadPool smartThreadPool = new SmartThreadPool(); // Max count threads, which can work together in one time var threadpoolMaxCountOfThreads = int.Parse(ConfigurationManager.AppSettings[SchedulerConfig.NumberOfSimultaneouslyProcessedMessagesAlias]); smartThreadPool.MaxThreads = (int)Math.Min(threadpoolMaxCountOfThreads, messagesStore.Messages.Count); // Parallel processing by using SmartThreadPool foreach (ArrivalMessage message in messagesStore.Messages) { smartThreadPool.QueueWorkItem(new WorkItemCallback(this.callback), new ThreadInfo { Message = message, ValidationSettings = validationSettings.Where(x => x.ProductId == message.ProductId).ToList(), }); } smartThreadPool.WaitForIdle(); smartThreadPool.Shutdown(); // Then we delete all files, if their count more than limit(another way it can call OutOfMemoryException) foreach (var checkingProductId in checkingProductIds) { _downloadService.CheckTmpFilesDir(checkingProductId); } } catch (Exception er) { var errorMessage = GetExceptionInfo(er); _log.Error(errorMessage); } }
void wiTimer_Tick(object sender, EventArgs e) { int count = Convert.ToInt32(spnWorkItemsPerSecond.Value); int sleepDuration = Convert.ToInt32(spnWorkItemDuration.Value); //Debug.WriteLine(string.Format("{0}: C = {1}, S = {2}", DateTime.Now.ToString("HH:mm:ss"), count, sleepDuration)); for (int i = 0; i < count; i++) { _stp.QueueWorkItem(DoWork, sleepDuration); } }
public void SetMaxQueueLength_IncreasedFromZero_AllowsLargerQueue() { var info = new STPStartInfo { MinWorkerThreads = 1, MaxWorkerThreads = 1, MaxQueueLength = 0, }; var pool = new SmartThreadPool(info); pool.Start(); try { pool.QueueWorkItem(SleepForOneSecond); // Picked up by waiter. } catch (QueueRejectedException e) { throw new Exception("Caught QueueRejectedException too early: ", e); } try { pool.QueueWorkItem(SleepForOneSecond); } catch (QueueRejectedException) { // Expected Assert.True(true); } pool.MaxQueueLength = 1; // Don't wait for worker item to complete, the queue should have immediately increased its allowance. var workItem = pool.QueueWorkItem <object>(ReturnNull); // If rejected, an exception would have been thrown instead. Assert.IsTrue(workItem.GetResult() == null); }
private void button3_Click(object sender, EventArgs e) { for (int i = 0; i < 1000; i++) { smartThreadPool.QueueWorkItem(() => { //模拟计算较长时间 Thread.Sleep(1000); handler(DateTime.Now.ToString()); }); } }
/// <summary> /// A Image from an HTTP source using a local cache /// </summary> /// <param name="url">Url to Image</param> /// <param name="preLoad">if true, image gets loaded at initialization, otherwise when accessing property ImageSource</param> /// <param name="validity">time in seconds until local cache gets updated by remote source. Default is 1 week</param> public CachedImage(string url, bool preLoad = false, long validity = 60 * 60 * 24 * 7) { _dispatcher = Dispatcher.CurrentDispatcher; Url = url; Validity = validity; if (preLoad) { _threadPool.QueueWorkItem(LoadBitmap); } }
public void ExceptionType() { SmartThreadPool smartThreadPool = new SmartThreadPool(); var workItemResult = smartThreadPool.QueueWorkItem(new Func <int>(ExceptionMethod)); smartThreadPool.WaitForIdle(); Assert.IsInstanceOf <System.NotImplementedException>(workItemResult.Exception); smartThreadPool.Shutdown(); }
/// <summary> /// Starts the tasks execution. /// </summary> /// <returns>If has reach the timeout false, otherwise true.</returns> public override bool Start() { base.Start(); m_threadPool = new SmartThreadPool(); try { m_threadPool.MinThreads = MinThreads; m_threadPool.MaxThreads = MaxThreads; var workItemResults = new IWorkItemResult[Tasks.Count]; for (int i = 0; i < Tasks.Count; i++) { var t = Tasks[i]; workItemResults[i] = m_threadPool.QueueWorkItem(new WorkItemCallback(Run), t); } m_threadPool.Start(); // Timeout was reach? if (!m_threadPool.WaitForIdle(Timeout.TotalMilliseconds > int.MaxValue ? int.MaxValue : Convert.ToInt32(Timeout.TotalMilliseconds))) { if (m_threadPool.IsShuttingdown) { return(true); } else { m_threadPool.Cancel(true); return(false); } } foreach (var wi in workItemResults) { Exception ex; wi.GetResult(out ex); if (ex != null) { throw ex; } } return(true); } finally { m_threadPool.Shutdown(true, 1000); m_threadPool.Dispose(); IsRunning = false; } }
private void ScheduleTileRequest(TileInfo tileInfo) { if (!HasTileAlreadyBeenRequested(tileInfo.Index)) { activeTileRequests.TryAdd(tileInfo.Index, 1); var threadArguments = new object[] { tileInfo }; threadPool.QueueWorkItem(GetTileOnThread, threadArguments); } }
/*! * Opens the COM port and creates a thread that will * process all incoming serial data */ public void Open() { //_serialPort.PortName = portName; //_serialPort.BaudRate = baudrate; //_serialPort.Open(); _smartThreadPool = SmartThreadPoolSingleton.GetInstance(); _smartThreadPool.Name = "ReceiveThreadedData"; IWorkItemResult wir = _smartThreadPool.QueueWorkItem( new WorkItemCallback(this.ReceiveThreadedData), _serialPort); }
public static void Run(SmartThread.Action task1) { SmartThreadPool st = new SmartThreadPool(); IWorkItemResult t1 = st.QueueWorkItem(() => { task1(); }); bool success = SmartThreadPool.WaitAll(new IWorkItemResult[] { t1 }); //if (success) result1 = (int)wir1.Result; st.Shutdown(); }
public void UserOnLine(User user) { LoggerHelper.Instance.Warning(user.Account + "上线了"); try { smartThreadPool.QueueWorkItem(OnUserOnLined, new EventArgs <User>(user)); } catch (Exception ex) { LoggerHelper.Instance.Error(ex); } }
public void DoWork() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemResult wir1 = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork1), null); IWorkItemResult wir2 = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork2), null); bool success = SmartThreadPool.WaitAll(new IWorkItemResult [] { wir1, wir2 }); if (success) { int result1 = (int)wir1.Result; int result2 = (int)wir2.Result; } smartThreadPool.Shutdown(); }
private void CheckSinglePriority(ThreadPriority threadPriority) { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.ThreadPriority = threadPriority; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); IWorkItemResult wir = stp.QueueWorkItem(new WorkItemCallback(GetThreadPriority)); ThreadPriority currentThreadPriority = (ThreadPriority)wir.GetResult(); Assert.AreEqual(threadPriority, currentThreadPriority); }
/// <summary> /// </summary> /// <param name="args"></param> internal static void Main(string[] args) { var stopwatch = new Stopwatch(); stopwatch.Start(); Config = new Configuration(); Console.WriteLine($"Creating thread pool with {Config.Workers} workers"); Pool = new SmartThreadPool(new STPStartInfo { AreThreadsBackground = true, IdleTimeout = 600000, MaxWorkerThreads = 32, MinWorkerThreads = Config.Workers, ThreadPriority = ThreadPriority.AboveNormal }); // Redis.Initialize(Config); SQL.Initialize(Config); if (Config.PopulateLeaderboards) { Pool.QueueWorkItem(Leaderboard.Populate); } if (Config.FixMultiplePersonalBestScores) { Pool.QueueWorkItem(Scores.FixMultiplePersonalBests); } if (Config.SyncScoresWithRankedStatus) { Pool.QueueWorkItem(Scores.SyncScoresWithRankedStatus); } if (Config.RecalculateOverallAccuracy) { Pool.QueueWorkItem(Accuracy.RecalculateOverall); } if (Config.RecalculateOverallPerformanceRating) { Pool.QueueWorkItem(PerformanceRating.RecalculateOverall); } if (Config.FixTotalHitCount) { Pool.QueueWorkItem(TotalHits.Recalculate); } while (Pool.CurrentWorkItemsCount != 0) { } stopwatch.Stop(); Console.WriteLine($"Cron completed in {stopwatch.ElapsedMilliseconds / 1000f} sec", Color.LimeGreen); }
public void GoodPostExecute() { SmartThreadPool stp = new SmartThreadPool(); stp.QueueWorkItem( new WorkItemCallback(DoWork), null, new PostExecuteWorkItemCallback(DoPostExecute)); stp.WaitForIdle(); stp.Shutdown(); }
/// <summary> /// Requests and asset and responds asynchronously /// </summary> /// <param name="assetID"></param> /// <param name="args"></param> public void RequestAsset(OpenMetaverse.UUID assetID, AssetRequestInfo args) { _threadPool.QueueWorkItem(() => { try { AssetBase asset = GetAssetInternal(assetID); if (asset != null) { _receiver.AssetReceived(asset, args); } else { _receiver.AssetNotFound(assetID, args); } } catch (Exception e) { _receiver.AssetError(assetID, e, args); } }); }
// Can't run this test, StackOverflowException crashes the application and can't be caught and ignored //[Test] public void NotTestThreadsMaxStackSize() { STPStartInfo stpStartInfo = new STPStartInfo() { MaxStackSize = 64 * 1024, }; SmartThreadPool stp = new SmartThreadPool(stpStartInfo); stp.Start(); IWorkItemResult <bool> wir = stp.QueueWorkItem(() => AllocateBufferOnStack(10 * 1024)); bool result = wir.GetResult(); Assert.IsTrue(result); wir = stp.QueueWorkItem(() => AllocateBufferOnStack(1000 * 1024)); result = wir.GetResult(); Assert.IsFalse(result); }
void Start() { planetCount = spaceGeneral.spaceDB.minPlanetCount; planetList = new ConcurrentDictionary <Vector3Int, Planet>(); // Запускается поток, ищущий планеты smartThreadPool = new SmartThreadPool(); smartThreadPool.QueueWorkItem(PlanetListener); // Запускается корутина, отдающая планеты в ScreenManager StartCoroutine(PlanetReturning(returnTime)); }
public async Task Execute(IJobExecutionContext context) { if (!IsRunning) { IsRunning = true; OnStart(context); var task = Task.Factory.StartNew(() => { var requests = GetRequests(); var stpStartInfo = new STPStartInfo { IdleTimeout = 3000, MaxWorkerThreads = MaxWorkerThreads, MinWorkerThreads = 0 }; var pool = new SmartThreadPool(stpStartInfo); var waits = new List <IWorkItemResult>(); foreach (var fr in requests) { if (fr.Request.Headers.Count(m => m.Name == "Referer") == 0) { fr.Request.Headers.Add(new WebHeader("Referer", fr.Request.Uri.AbsoluteUri)); } var item = pool.QueueWorkItem((u) => { var response = DoTask((Request)u.Request); Save(u, response); }, fr); waits.Add(item); } SmartThreadPool.WaitAll(waits.ToArray()); pool.Shutdown(true, 1000); pool.Dispose(); pool = null; waits.Clear(); }); await task; IsRunning = false; } }
public void SetMaxQueueLength_FromNullToZero_DisablesQueueing() { Assert.Throws <QueueRejectedException>(() => { var info = new STPStartInfo { MinWorkerThreads = 1, MaxWorkerThreads = 1, }; var pool = new SmartThreadPool(info); pool.Start(); try { pool.QueueWorkItem(SleepForOneSecond); // Picked up by waiter. pool.QueueWorkItem(SleepForOneSecond); // Queued. } catch (QueueRejectedException e) { throw new Exception("Caught QueueRejectedException too early: ", e); } pool.MaxQueueLength = 0; Thread.Sleep(2100); // Let the work items complete. try { pool.QueueWorkItem(SleepForOneSecond); // Picked up by waiter. } catch (QueueRejectedException e) { throw new Exception("Caught QueueRejectedException too early: ", e); } pool.QueueWorkItem(SleepForOneSecond); // Rejected (max queue length is zero). }); }
public void WorkItemWaitCanceling() { SmartThreadPool smartThreadPool = new SmartThreadPool(); ManualResetEvent cancelWaitHandle = new ManualResetEvent(false); // Queue a work item that will occupy the thread in the pool IWorkItemResult wir1 = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork), null); // Queue another work item that will wait for the first to complete IWorkItemResult wir2 = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.SignalCancel), cancelWaitHandle); try { wir1.GetResult(System.Threading.Timeout.Infinite, true, cancelWaitHandle); } finally { smartThreadPool.Shutdown(); } }
public void ChainedDelegatesCallback() { SmartThreadPool stp = new SmartThreadPool(); WorkItemCallback workItemCallback = new WorkItemCallback(DoWork); workItemCallback += new WorkItemCallback(DoWork); stp.QueueWorkItem(workItemCallback); stp.WaitForIdle(); stp.Shutdown(); }
// Handle the processing of a request in here. private void ListenerCallback(IAsyncResult asyncResult) { var listener = asyncResult.AsyncState as HttpListener; HttpListenerContext context; if (listener == null) { return; } var isListening = listener.IsListening; try { if (!isListening) { log.DebugFormat("Ignoring ListenerCallback() as HttpListener is no longer listening"); return; } // The EndGetContext() method, as with all Begin/End asynchronous methods in the .NET Framework, // blocks until there is a request to be processed or some type of data is available. context = listener.EndGetContext(asyncResult); } catch (Exception ex) { // You will get an exception when httpListener.Stop() is called // because there will be a thread stopped waiting on the .EndGetContext() // method, and again, that is just the way most Begin/End asynchronous // methods of the .NET Framework work. string errMsg = ex + ": " + isListening; log.Warn(errMsg); return; } finally { // Once we know we have a request (or exception), we signal the other thread // so that it calls the BeginGetContext() (or possibly exits if we're not // listening any more) method to start handling the next incoming request // while we continue to process this request on a different thread. listenForNextRequest.Set(); } if (Config.DebugMode) { log.DebugFormat("{0} Request : {1}", context.Request.UserHostAddress, context.Request.RawUrl); } RaiseReceiveWebRequest(context); threadPoolManager.QueueWorkItem(() => InitTask(context)); }
public void DoWork(int[] numbers) { SmartThreadPool smartThreadPool = new SmartThreadPool(); // Queue the work item IWorkItemResult <double> wir = smartThreadPool.QueueWorkItem(new Func <int[], double>(CalcAverage), numbers); // Do some other work here // Get the result of the operation double average = wir.Result; smartThreadPool.Shutdown(); }
public void DoWork() { SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemResult wir1 = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork1), null); IWorkItemResult wir2 = smartThreadPool.QueueWorkItem(new WorkItemCallback(this.DoSomeWork2), null); IWorkItemResult [] wirs = new IWorkItemResult [] { wir1, wir2 }; int index = SmartThreadPool.WaitAny(wirs); if (index != WaitHandle.WaitTimeout) { int result = (int)wirs[index].Result; } smartThreadPool.Shutdown(); }
public static void RunThreads(Action <byte[], byte[], int> function, List <byte[]> arrayList, byte[] arrayRgbColorBytes, int threadsAmount) { SmartThreadPool smartThreadPool = new SmartThreadPool(60 * 1000, threadsAmount, threadsAmount); foreach (var byteArray in arrayList) { smartThreadPool.QueueWorkItem(function, byteArray, arrayRgbColorBytes, byteArray.Length); } smartThreadPool.Start(); smartThreadPool.WaitForIdle(); smartThreadPool.Shutdown(); }
public void QueueWorkItem_WhenMaxIsNull_Queues() { var info = new STPStartInfo { MaxQueueLength = null, }; var pool = new SmartThreadPool(info); pool.Start(); var workItem = pool.QueueWorkItem <object>(ReturnNull); // If rejected, an exception would have been thrown instead. Assert.IsTrue(workItem.GetResult() == null); }
private void OnShown() { var sortedDirs = from o in Dirs orderby o.Key select o; Parallel.ForEach(Dirs.Keys, d => { waitQueue.Add(pool.QueueWorkItem(() => Console.WriteLine("++" + Invoke(new invAddDir(clistDirs.Items.Add), new object[] { GetShorten(d), false })))); }); }
private void UploadChunkList(ref List <ChunkInfo> chunkList_toUpload, string filePath, string blobName) { SmartThreadPool threadPool = new SmartThreadPool(); threadPool.MaxThreads = MaxConcurrentUploadThreads; foreach (ChunkInfo chunk in chunkList_toUpload) { ChunkInfo chunkToUpload = chunk; IWorkItemResult wir1 = threadPool.QueueWorkItem(new WorkItemCallback(this.UploadChunk_Worker), new Tuple <string, string, ChunkInfo>(filePath, blobName, chunkToUpload)); //UploadChunk(filePath, blobName, ref chunkToUpload); } threadPool.Start(); threadPool.WaitForIdle(); threadPool.Shutdown(); }
/// <summary> /// Example of how to queue a work item and then cancel it while it is in the queue. /// </summary> private bool DoTestPostExecuteWithCancel(CallToPostExecute callToPostExecute, bool answer) { // Create a SmartThreadPool with only one thread. // It just to show how to use the work item canceling feature SmartThreadPool smartThreadPool = new SmartThreadPool(10 * 1000, 1); bool success = false; PostExecuteResult postExecuteResult = new PostExecuteResult(); // Queue a work item that will occupy the thread in the pool smartThreadPool.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), null); // Queue another work item that will wait for the first to complete IWorkItemResult wir = smartThreadPool.QueueWorkItem( new WorkItemCallback(this.DoSomeWork), postExecuteResult, new PostExecuteWorkItemCallback(this.DoSomePostExecuteWork), callToPostExecute); // Wait a while for the thread pool to start executing the first work item Thread.Sleep(100); // Cancel the second work item while it still in the queue if (wir.Cancel()) { success = (postExecuteResult.wh.WaitOne(1000, true) == answer); } smartThreadPool.Shutdown(); return(success); }