M
M
Maxim Nikitin2014-08-04 10:24:53
.NET
Maxim Nikitin, 2014-08-04 10:24:53

How to increase the number of running threads?

There is an application that downloads files from the server using the WebClient class (training task). To ensure multithreading, Thread, ThreadPool, BackgroundWorker, Delegate.BeginInvoke are used (the method is chosen by the user). At startup, only two downloads can work at the same time, the rest are waiting in the queue. How to increase the number of executable threads?
TPL do not offer :)

public class Download : IManager
    {
        private Uri uri;
        private string file;
        public event EventHandler<DownloadInfoEventArgs> AlarmStartedDownload;
        public Download(Uri uri, string file)
        {
            this.uri = uri;
            while (true)
            {
                if (System.IO.File.Exists(file))
                {
                    file += "_";
                }
                else
                {
                    this.file = file;
                    break;
                }
            }
        }
        public string FileName
        {
            get { return file; }
        }

        public void StartDownload()
        {
            WebClient webClient = new WebClient();
            Thread thread = new Thread(()=> webClient.DownloadFileAsync(uri, file));   //запуск потока
            thread.Start();
            webClient.DownloadProgressChanged += (s, e) => OnAlarmDownloadStarted(e.ProgressPercentage);
        }
        public virtual void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)    //события, не обращайте внимания ;)
        {
            OnAlarmDownloadStarted(e.ProgressPercentage);
        }
        public virtual void OnAlarmDownloadStarted(int progress)
        {
            AlarmStartedDownload.Invoke(this, new DownloadInfoEventArgs(progress));
        } 
    }

The code with other ways to start multithreading, as you understand, is similar.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Mozhaykin, 2014-08-04
@MaxSter

Why manually create threads if DownloadFileAsync is used?
In this example, the issue is not with the threads, but with the connection limit. You can change it in App.config:

<configuration>
  <system.net>
    <connectionManagement>
      <add address = "*" maxconnection = "100" />
    </connectionManagement>
  </system.net>
</configuration>

C
Cyril, 2014-08-04
@llirikkkk

In Windows, there is a limit on the number of simultaneously open tcp connections.
Read the wiki: goo.gl/osI2h6
One of the fix options: half-open.com/home_ru.htm
I'm not exactly sure, but you can try not to create a WebClient every time, but create one, and give it asynchronous tasks to download files.
And what's the point of doing that?
because it
already returns you a running separate thread when you download the file
, you get -> create a new thread, in which a new thread is created that performs the task.
just use the last option.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question