public void TestJoin() { SmartThreadPool stp = new SmartThreadPool(); SafeCounter sc = new SafeCounter(); stp.Join( sc.Increment, sc.Increment, sc.Increment); Assert.AreEqual(3, sc.Counter); for (int j = 0; j < 10; j++) { sc.Reset(); Action[] actions = new Action[1000]; for (int i = 0; i < actions.Length; i++) { actions[i] = sc.Increment; } stp.Join(actions); Assert.AreEqual(actions.Length, sc.Counter); } stp.Shutdown(); }
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(); }
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"); }