Edit:
I have noticed these lag spikes only occur while debugging in visual studio. If I run the .exe outside of Visual Stduio,the program doesnt use more than 3% of the CPU.Can anyone tell me why this is happening?
I have encountered a problem with Parallel processing.I'm using Parallel.For to check a large number of proxies (by making webrequests).This is my function:
private ConcurrentBag<string> TotalProxies = new ConcurrentBag<string>();
private void CheckProxies()
{
ParallelOptions pOptions = new ParallelOptions();
pOptions.MaxDegreeOfParallelism = 100;
int max = TotalProxies.Count;
Invoke(new Action(() => { lbl_Status.Text = "Checking"; }));
Parallel.For(0, max, pOptions, (index, loopstate) =>
{
string Proxy = TotalProxies.ElementAt(index);
if (WebEngine.IsProxyWorking(Proxy))
{
WorkingProxies.Add(Proxy);
workingp++;
Invoke(new Action(() =>
{
lstv_Working.Items.Add(Proxy);
lbl_Working.Text = workingp.ToString();
}));
}
checkedp++;
Invoke(new Action(() => { lbl_Checked.Text = checkedp.ToString(); }));
if (Stop)
loopstate.Stop();
});
Invoke(new Action(() => {
lbl_Status.Text = "Idle";
}));
}
My problem is the following:
The program works fine for the first 0-2000 requests,where the cpu usage is around 3-5%.Then,after 2-3 minutes, I have encountered massive and frequent lag spikes,causing the CPU usage to jump up to 100%. I have no idea why this is happening,since it worked fine until now.I hope someone can help me understand what causes this.
Here you can see my problem:
As promised an example with async/await, although seeing your update I'm not sure if it will make a difference. But since it won't fit inside a comment, posted it here ;)
Note, since I couldn't the test the actual code, I'm not sure about the efficiency. Your current code might perform better. But if the
IsProxyWorking
method could use actual async webcalls (I believe that code was previously included in your post), I believe the processing could really improve.