Answer the question
In order to leave comments, you need to log in
How to do multithreading with many requests to the server?
There is, for example, a task - you need to contact the server using the link example.com/?i=X 5000 times, where X is some string. There is a list of these links. And there is a maximum number of threads (eg 50).
So far everything is implemented and working like this
void managerThread()
{
while (true)
{
for (int i = threads.Count - 1; i >= 0; i--) //убирать выполненные потоки
if (!threads[i].IsAlive)
threads.RemoveAt(i);
if (threads.Count == 0)
lock (strings) //список строк
if (strings.Count == 0)
return;
Thread.Sleep(50);
if (threads.Count < threadsMax)
{
Thread thread = new Thread(someMethodDo); //запуск метода на обработку строки
thread.IsBackground = true;
thread.Priority = ThreadPriority.Lowest;
threads.Add(thread);
thread.Start();
}
}
}
Answer the question
In order to leave comments, you need to log in
Install Linux and using curl and gnu parallel don't write code at all :-) best way out. In general, just type c# threadpool into msdn and a bunch of ready-made examples are waiting for you.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question