private void btnStart_Click(object sender, EventArgs e) { txtLogs.Clear(); if (File.Exists(_proxyPath) == false) { return; } _proxies = new List <string>(); var proxies = File.ReadLines(_proxyPath); foreach (var proxy in proxies) { _proxies.Add(proxy.Trim()); } if (_proxies.Count == 0) { return; } _tokenSource = new CancellationTokenSource(); _token = _tokenSource.Token; Running(true); var listClientId = txtClientIDList.Text.Trim(); var clients = Regex.Split(listClientId, "\n", RegexOptions.Multiline); _threadPool = new SmartThreadPool() { Concurrency = 1, MaxThreads = 1, MinThreads = 1 }; var totalClient = 0; var timeout = int.Parse(numTimeout.Text); var proxyType = comboProxyType.Items[comboProxyType.SelectedIndex].ToString(); foreach (var _ in clients) { var client = _.Trim(); if (client.Length <= 0) { continue; } _threadPool.QueueWorkItem(DoWork, client, int.Parse(numThreads.Text), timeout, proxyType); totalClient++; } lbTotalProxy.Text = _proxies.Count.ToString(); lbTotalClient.Text = totalClient.ToString(); new Thread(() => { _threadPool.Start(); _threadPool.Join(); _threadPool.WaitForIdle(); _threadPool.Dispose(); _tokenSource.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); Running(false); }) { IsBackground = true }.Start(); }
protected virtual void Dispose(bool disposing) { if (_disposed) { return; } this._disposed = true; if (disposing) { //释放托管资源,系统调用时不进来。用于主动释放 ds = null; dicSendWindow = null; DB = null; try { threadHandlePackPLCComm.Abort(); } catch {} } //非托管对象释放,不管是手动还是系统都要执行; try { threadHandlePackPLCComm = null; smartThreadPool.Dispose(); Ax.Dispose(); } catch { } }
protected override void Dispose(bool disposing) { if (disposed) { return; } lock (this) { if (disposed) { return; } if (disposing) { threadPoolManager.Dispose(); } // new shared cleanup logic disposed = true; base.Dispose(disposing); } }
public async Task Execute(IJobExecutionContext context) { if (!IsRunning) { IsRunning = true; baseUrl = context.JobDetail.JobDataMap.Get("baseUrl").ToString(); proxyUrl = context.JobDetail.JobDataMap.Get("proxyUrl").ToString(); feedNode = context.JobDetail.JobDataMap.Get("node") as FeedNode; Logger.GetLogger(baseUrl).Info("start feed job execute"); var task = Task.Factory.StartNew(() => { var feeds = GetFeedJobs(baseUrl, proxyUrl, feedNode); var compile = new UrlCompile(); var stpStartInfo = new STPStartInfo { IdleTimeout = 3000, MaxWorkerThreads = 8, MinWorkerThreads = 0 }; var pool = new SmartThreadPool(stpStartInfo); var waits = new List <IWorkItemResult>(); foreach (var feed in feeds) { var addrs = compile.GetResult(feed.Address); Logger.GetLogger(baseUrl).Info("compile address " + feed.Address + " result " + string.Join(",", addrs)); foreach (var addr in addrs) { feed.Address = addr.ToString(); var item = pool.QueueWorkItem((u) => { DoTask(u, true); }, feed); waits.Add(item); } } SmartThreadPool.WaitAll(waits.ToArray()); pool.Shutdown(true, 1000); pool.Dispose(); pool = null; waits.Clear(); }); await task; IsRunning = false; Logger.GetLogger(baseUrl).Info("end feed job execute"); } }
/// <summary> /// Disposal of the instance. /// </summary> /// <param name="disposing">Determines whether or not dispose is being called or not.</param> protected virtual void Dispose(bool disposing) { if (_threadPool != null) { _threadPool.Dispose(); _threadPool = null; } }
/// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { if (m_threadPool != null) { m_threadPool.Dispose(); } GC.SuppressFinalize(this); }
public bool Stop() { if (this.curTaskGroup == null) { return(false); } GlobalVar.Instance.logger.Debug("开始尝试停止任务."); StopPerformance(); isProducerThreadRunning = false; isWorkerThreadRunning = false; isNoticeStop = false; if (performance != null) { GlobalVar.Instance.logger.Debug("最后统计."); performance.OnTick(); } try { if (tasksProducerThread != null) { tasksProducerThread.Join(); } if (smartThreadPool != null) { smartThreadPool.Shutdown(true, 1000); smartThreadPool.Dispose(); smartThreadPool = null; } GC.Collect(); GC.WaitForPendingFinalizers(); } catch (Exception e) { GlobalVar.Instance.logger.Error("释放回收出现错误.", e); return(false); } finally { GlobalVar.Instance.logger.Info("任务已停止."); this.curTaskGroup.Stop(); } if (tasksProducerThread != null) { tasksProducerThread.Abort(); tasksProducerThread = null; } curTaskGroup.Clear(); return(true); }
private void Stop_Click(object sender, RoutedEventArgs e) { running = false; workItemsProducerThread.Join(); _stp.Shutdown(true, 1000); _stp.Dispose(); _stp = null; GC.Collect(); GC.WaitForPendingFinalizers(); UpdateControls(false); }
/// <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; } }
public void Dispose() { exit = true; m_STP.Shutdown(true, 1000); m_STP.Dispose(); GC.Collect(); GC.WaitForPendingFinalizers(); queue.Shutdown(); }
private void btnStop_Click(object sender, System.EventArgs e) { running = false; workItemsProducerThread.Join(); _smartThreadPool.Shutdown(true, 1000); _smartThreadPool.Dispose(); _smartThreadPool = null; GC.Collect(); GC.WaitForPendingFinalizers(); UpdateControls(false); }
private void materialRaisedButton12_Click(object sender, EventArgs e) { stopTime = System.DateTime.Now; running = false; workItemsProducerThread.Join(); _smartThreadPool.Shutdown(true, 1000); _smartThreadPool.Dispose(); _smartThreadPool = null; GC.Collect(); GC.WaitForPendingFinalizers(); UpdateControls(false); }
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 Dispose() { if (!disposed) { disposed = true; sending = false; outQueue.Dispose(); threadProcSendPackets.Join(); m_STP.Dispose(); } }
void IDisposable.Dispose() { if (_volatileCache == null) { return; } _volatileCache.Clear(); _volatileCache = null; _provider = null; _permaCache = null; _threadPool.Dispose(); _threadPool = null; }
public async Task Execute(IJobExecutionContext context) { if (!IsRunning) { IsRunning = true; baseUrl = context.JobDetail.JobDataMap.Get("baseUrl").ToString(); try { MoveDelayFeed(); var task = Task.Factory.StartNew(() => { var files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + @"snapshot"); var stpStartInfo = new STPStartInfo { IdleTimeout = 3000, MaxWorkerThreads = 8, MinWorkerThreads = 0 }; var pool = new SmartThreadPool(stpStartInfo); var waits = new List <IWorkItemResult>(); foreach (var file in files) { var item = pool.QueueWorkItem((fileName) => { DoTask(fileName); }, file); waits.Add(item); } SmartThreadPool.WaitAll(waits.ToArray()); pool.Shutdown(true, 1000); pool.Dispose(); pool = null; waits.Clear(); }); await task; } catch { } IsRunning = false; } }
public async Task Execute(IJobExecutionContext context) { if (!IsRunning) { IsRunning = true; var task = Task.Factory.StartNew(() => { var proxyUrl = context.JobDetail.JobDataMap.Get("proxyUrl").ToString(); var node = context.JobDetail.JobDataMap.Get("node") as FeedNode; var feeds = GetFeedJobs(proxyUrl, node); var stpStartInfo = new STPStartInfo { IdleTimeout = 3000, MaxWorkerThreads = 8, MinWorkerThreads = 0 }; var pool = new SmartThreadPool(stpStartInfo); var waits = new List <IWorkItemResult>(); foreach (var feed in feeds) { var item = pool.QueueWorkItem((u) => { DoTask(u, true); }, feed); waits.Add(item); } SmartThreadPool.WaitAll(waits.ToArray()); pool.Shutdown(true, 1000); pool.Dispose(); pool = null; waits.Clear(); }); await task; IsRunning = false; } }
public async Task Execute(IJobExecutionContext context) { if (!IsRunning) { IsRunning = true; OnStart(context); var task = Task.Factory.StartNew(() => { var snapshots = GetSnapshot(); var stpStartInfo = new STPStartInfo { IdleTimeout = 3000, MaxWorkerThreads = MaxWorkerThreads, MinWorkerThreads = 0 }; var pool = new SmartThreadPool(stpStartInfo); var waits = new List <IWorkItemResult>(); foreach (var snapshot in snapshots) { var item = pool.QueueWorkItem((u) => { DoTask(u); }, snapshot); waits.Add(item); } SmartThreadPool.WaitAll(waits.ToArray()); pool.Shutdown(true, 1000); pool.Dispose(); pool = null; waits.Clear(); }); await task; IsRunning = false; } }
protected virtual void Dispose(bool disposing) { if (IsDisposed) { return; } if (disposing) { volatileCache.Clear(); threadPool.Dispose(); threadPool = null; volatileCache = null; provider = null; persistentCache = null; } IsDisposed = true; }
public void Start(LicenseInfo licenseInfo) { if (isRunning) { return; } /* * 1. Get all active clients from db * 2. generate multi-thread for boardcasting message */ int activeClient = 0; try { isRunning = true; using (var db = new MyDbContext()) { #region Prepare Parameters db.Configuration.ProxyCreationEnabled = false; db.Configuration.LazyLoadingEnabled = false; var listClients = ListClientWithIPAddress(licenseInfo, out activeClient, true); List <RmsClient> broadcastClient = new List <RmsClient>(); foreach (var client in listClients) { if (client.UseLocalInfo == true) { var location = db.RmsLocations.Find(client.LocationId); if (location != null) { if (CanBroadCast(location)) { broadcastClient.Add(client); } } } else { broadcastClient.Add(client); } } // Log into RMS_Log_Monitoring int refID = AddLogMonitoring(activeClient, true, null); #region Initial Thread Pool var maxThreadConfig = db.RmsSystemConfigs.Find("MonitoringEngine.MaxThread"); int minThread = 1; int maxThread = 1; if (maxThreadConfig != null) { maxThread = Convert.ToInt32(maxThreadConfig.Value ?? maxThreadConfig.DefaultValue); if (maxThread > broadcastClient.Count) { maxThread = broadcastClient.Count; } } if (broadcastClient.Count > 0) { STPStartInfo stpStartInfo = new STPStartInfo(); stpStartInfo.MaxWorkerThreads = maxThread; stpStartInfo.IdleTimeout = 60 * 1000; stpStartInfo.MinWorkerThreads = minThread; stpStartInfo.PerformanceCounterInstanceName = "RMSCounter"; stpStartInfo.EnableLocalPerformanceCounters = true; stp = new SmartThreadPool(stpStartInfo); } #endregion foreach (var client in broadcastClient) { RmsClient _client = client; Amib.Threading.Action broadcastAliveMessage = () => { BroadcastAliveMessage(_client, refID); }; stp.QueueWorkItem(broadcastAliveMessage); } #endregion } } catch (Exception ex) { string exLicense = "License is invalid"; if (ex.Message.ToLower().IndexOf(exLicense.ToLower()) > -1) { // Log into RMS_Log_Monitoring AddLogMonitoring(activeClient, false, ex.Message); } throw new RMSWebException(this, "0500", "Start failed. " + ex.Message, ex, false); } finally { if (stp != null) { stp.WaitForIdle(); System.Threading.Thread.Sleep(1000); if (!stp.IsShuttingdown) { stp.Shutdown(); } stp.Dispose(); } isRunning = false; } }
private void ScriptEventsThread() { while (m_running) { // Check if there are free threads in the script thread pool if (m_eventThreadPool.MaxThreads - m_eventThreadPool.InUseThreads > 0) { // Check HTTP requests // Check XML-RPC requests // Check listeners // Check timers CheckTimerEvents(); // Check sensors // Check grid requests // Dequeue the next script event LSLScriptInstance instance = null; EventParams scriptEvent = null; lock (m_syncRoot) { // Try to dequeue the next script with a pending event, // then the next pending event for that script if (m_scriptsWithEvents.Count > 0) { instance = m_scriptsWithEvents.Dequeue(); if (instance != null) { scriptEvent = instance.DequeueEvent(); } } } if (instance != null && scriptEvent != null) { if (scriptEvent.EventName == "timer") { instance.IsTimerEventQueued = false; } if (scriptEvent.EventName == "control") { instance.ControlEventsQueued--; } if (scriptEvent.EventName == "collision") { instance.IsCollisionEventQueued = false; } if (scriptEvent.EventName == "state") { // FIXME: //instance.SetState(scriptEvent.EventName); //LSLEventFlags flags = GetEventsForState(instance.ID, instance.State); } else { string methodName = instance.State + "_event_" + scriptEvent.EventName; // Convert the method arguments to LSL types and queue // this event handler on the thread pool ConvertParamsToLSLTypes(ref scriptEvent.Params); instance.CurrentExecution = m_eventThreadPool.QueueWorkItem(RunScriptMethod, instance, methodName, scriptEvent.Params); } } } else { m_log.Debug("No free threads in the script thread pool, sleeping..."); } // TODO: Dynamic sleeping interval? Thread.Sleep(XENGINE_LONG_SLEEP_INTERVAL); m_scheduler.ThreadKeepAlive(); } m_eventThreadPool.Dispose(); m_scheduler.RemoveThread(); }
public static void destory() { threadPool.Dispose(); }
private void DoWork(string clientid, int totalWorker) { Logs($"ID '{clientid}': Start"); _max = int.Parse(numMaxGB.Text); _earned = 0; _totalDone = 0; var total = 0; _threads = new SmartThreadPool() { Concurrency = totalWorker, MaxThreads = totalWorker, MinThreads = totalWorker }; for (var i = 0; i < totalWorker; i++) { _threads.QueueWorkItem(() => { while (true) { lock (_lock) { if (total >= _max) { return; } } if (_token.IsCancellationRequested) { return; } HttpRequest request = null; StringContent body = null; var proxy = RandomProxy(); if (proxy == "") { return; } try { var proxyInfo = ProxyClient.Parse(ProxyType.HTTP, proxy); request = new HttpRequest { Proxy = proxyInfo, KeepAlive = false, ConnectTimeout = 2000 }; request.AddHeader("Content-Type", "application/json"); body = new StringContent(JsonConvert.SerializeObject(new { referrer = clientid })); if (total <= _max) { request.Post("https://api.cloudflareclient.com/v0a778/reg", body); } lock (_lock) { total++; _earned++; UpdateEarned(); } } catch (ProxyException proxyException) { _proxies.Remove(proxy); Logs($"Proxy Error: {proxy} - {proxyException.Message}"); } catch (HttpException httpException) { Logs($"Http Error: {proxy} - {httpException.Message}"); } catch (Exception e) { Logs($"Error: {e.Message}"); } finally { request?.Dispose(); body?.Dispose(); } } }); } _threads.Start(); _threads.Join(); _threads.WaitForIdle(); _threads.Dispose(); UpdateTotalDone(); Logs($"ID {clientid}: Earned {_earned} GB"); }