Am I missing something here? I've tried putting a set value such as 1 or 4 in the "MaxDegreeOfParallelism =" option but in the example below it is being assigned by and Up/down selector.
Is there a reason my MaxDegreeOfParallelism Parallel option isn't working? Right now the program seems to use as many thread as it wants. I also have a memory leak big time and I am assuming this is due to the Parallel.ForEach loop because the leak wasn't there before.
private void NumericUpDown1_ValueChanged(object sender, EventArgs e)
{
threadCount = threadCountUD.Value;
threadC = Decimal.ToInt32(threadCount);
}
private void MainWork()
{
var maxThread = new ParallelOptions
{
MaxDegreeOfParallelism = threadC,
};
var cookies = new CookieContainer();
proxies = File.ReadAllLines(proxPath);
fileInfo = File.ReadAllLines(path);
var proxAndNames = proxies.Zip(fileInfo, (n, w) => new { Proxies = n, Name = w });
bool repeat = true;
Parallel.ForEach(proxAndNames.AsParallel().ToArray(),maxThread, async (var) =>
{ while (repeat)
{
if (stopBtn is true)
{
break;
}
var httpClientHandler = new HttpClientHandler
{
Proxy = new WebProxy(var.Proxies),
AllowAutoRedirect = true,
CookieContainer = cookies,
UseCookies = false,
UseDefaultCredentials = false,
AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
UseProxy = true
};
using (var client = new HttpClient(httpClientHandler, false))
{
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0");
client.BaseAddress = new Uri("https://www.google.com/");
var cts = new CancellationTokenSource();
try
{
HttpResponseMessage response = await client.GetAsync(var.Name.AsParallel() + "/");
response.EnsureSuccessStatusCode();
if (response.IsSuccessStatusCode)
{
string grabbed = await client.GetStringAsync(var.Name.AsParallel() + "/");
}
}
catch (TaskCanceledException)
{
if (!cts.Token.IsCancellationRequested)
{
}
else
{
// Cancelled for some other reason
}
}
catch (HttpRequestException ex)
{
proxBox.Invoke(new Action(() => proxBox.Text += ex.Message + System.Environment.NewLine));
}
finally
{
client.Dispose();
cts.Dispose();
}
}
}
});
}