Toqe / Downloader

A library for resuming and multi-part/multi-threaded downloads in .NET written in C#
MIT License
136 stars 50 forks source link

How do I pause and resume a MultipartDownload? #2

Closed zhaxiu3 closed 7 years ago

zhaxiu3 commented 7 years ago

Hi Toqe, Thank you for this excellent work. I followed your example and succeeded to build a multipartdownload. The problem is when i stop it and start it again, an exception comes out and says InvalidOperationException: Invalid state: Running Toqe.Downloader.Business.Download.AbstractDownload.Start () Do you have any ideas?

Toqe commented 7 years ago

Hi zhaxiu3,

I'm glad you like it.

The smaller issue is that the exception contains the wrong state "Running" - that's because of a bug in the MultiPartDownload, which did not correctly set the state when being stopped. I've just fixed this, so the exception will contain Invalid state: Stopped now.

The other issue is that the download objects are designed to be used only once through the states initialized, running to finished, stopped or canceled. So they should not be restarted after being stopped. That design makes the internal state handling a bit simpler. But you can easily start a new MultiPartDownload continueing the work of a previously stopped. You only need to keep the download's AlreadyDownloadedRanges which contain all necessary information about the download's progress before being stopped. Using the code in the Downloader.Example Programs.cs, you can stop and restart the download like this:

download.Stop();
download.DetachAllHandlers();

alreadyDownloadedRanges = download.AlreadyDownloadedRanges;

download = new MultiPartDownload(url, bufferSize, numberOfParts, resumingDlBuilder, requestBuilder, dlChecker, alreadyDownloadedRanges);
speedMonitor.Attach(download);
progressMonitor.Attach(download);
dlSaver.Attach(download);
download.DownloadCompleted += OnCompleted;
download.Start();
zhaxiu3 commented 7 years ago

Thanks for your reply!