A
A
ASTREL2014-08-06 20:23:04
C++ / C#
ASTREL, 2014-08-06 20:23:04

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();
                }
            }
        }

And already in the method itself, a string is removed from the list, processed. The solution works, but I would like not to create another cycle, but a real example of a ThreadPool or TaskScheduler, for a similar task, with a given number of threads to process X requests.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Starkov, 2014-08-06
@icelaba

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 question

Ask a Question

731 491 924 answers to any question