I have a class method that i am trying to execute concurrently in almost 100 threads. This method is basically about HTTPWebRequests and database R/W My application is a console application which spins up threads to call this method.
Now when i dont thread and run 35-45 copies or instance of my console application, i get a faster/better performance, however when i run 100 threads, the performance drops by as much as 50%.
I have tried threadpool, array of background workers, threads and even tasks, but something is not working the way it should. This is how i am invoking a static method in main class that in turn class the other class method.
while (flagProcess)
{
if (RAMPRead.CheckJobQueue())
{
bool allThreadsinUse = true;
for (int i = 0; i < ts.Length; i++)
{
if (ts[i] == null || ts[i].ThreadState != ThreadState.Running)
{
ts[i] = new Thread(() =>
{
RAMPit(session);
});
ts[i].Start();
ts[i].IsBackground = true;
allThreadsinUse = false;
while (!ts[i].IsAlive) { }
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
}
}
if (!allThreadsinUse)
{
Thread.Sleep(10000);
MemoryManagement.FlushMemory();
}
}
else
{
Thread.Sleep(10000);
MemoryManagement.FlushMemory();
}
}
private static void RAMPit(object s)
{
string result = string.Empty;
Session outsession = new Session();
_sem.Wait();
using (z z = new z("user", "pass"))
{
outsession = z.ProcessNext((Session)s, "user", "pass");
session = outsession;
}
_sem.Release();
MemoryManagement.FlushMemory();
//return result;
}
What am i doing wrong here OR how can i get to run many threads concurrently for faster output.