public DownloaderForm(string storyUrl)
 {
     InitializeComponent();
     Settings = SettingForm.GetSetting();
     txtUrl.Text = storyUrl;
     this.Downloader = Downloader.Resolve(txtUrl.Text);
     downloadNow = true;
 }
        private void DownloadChapter(ChapterInfo chapInfo, Downloader downloader)
        {
            if (chapInfo.Pages == null) return;

            SmartThreadPool smartThreadPool = new SmartThreadPool()
            {
                MaxThreads = Settings.UseMultiThreadToDownloadChapter?Settings.Threads:1
            };

            List<IWorkItemResult> workerItems = new List<IWorkItemResult>();

           CreateChapterFolder(chapInfo);

           this.Invoke(new MethodInvoker(delegate()
           {
               lblStatus.Text = "Downloading : " + chapInfo.Name + "[" + chapInfo.Url + "]";
           }));
            int count = 0;
            long size = 0;
            
            int toProcess = chapInfo.Pages.Count;
            int index = 1;
           
            foreach (string pageUrl in chapInfo.Pages)
            {
                IWorkItemResult wir1 = smartThreadPool.QueueWorkItem(new
                        WorkItemCallback(delegate(object state)
                        {

                            try
                            {
                                string filename = downloader.DownloadPage(state.ToString(), Settings.RenamePattern.Replace("{{PAGENUM}}", (index++).ToString("D2")), chapInfo.Folder, chapInfo.Url);

                                var file = File.Open(filename, FileMode.Open);

                                lock (updateUIObj)
                                {
                                    size += file.Length;
                                    //total += file.Length;
                                    file.Close();
                                    count++;
                                    chapInfo.Size = size;
                                    chapInfo.DownloadedCount = count;
                                    RefreshData(chapInfo);
                                }
                            }
                            catch{
                            }
                            finally
                            {
                                RefreshData(chapInfo);
                            }
                            return 0;
                        }), pageUrl);
                workerItems.Add(wir1);

            }

            bool success = SmartThreadPool.WaitAll(workerItems.ToArray());

            smartThreadPool.Shutdown();
        }
 internal void SetDownloader(Downloader dl)
 {
     AddChildForm(dl.Name, dl);
     this.WindowState = FormWindowState.Maximized;
 }
 private void AddChildForm(string title, Downloader dl)
 {
     DownloaderForm childForm = new DownloaderForm();
     childForm.MdiParent = this;
     childForm.Downloader = dl;
     childForm.Text = title;
     childForm.WindowState = FormWindowState.Minimized;
     childForm.Show();
     childForm.WindowState = FormWindowState.Maximized;
 }
        public ReadOnlineForm(string sFilePath)
        {
            InitializeComponent();
            InitializeChildForm();
            SmartThreadPool pool = new SmartThreadPool() { MaxThreads = 8 };
            if (sFilePath.StartsWith("http"))
            {
                downloader = Downloader.Resolve(sFilePath);
                 m_sFilePaths = downloader.GetPages(sFilePath);

                //Precache data
                 //new Thread(new ThreadStart(delegate() {
                     for (int i = 0; i < m_sFilePaths.Count; i++)
                     {
                         pool.QueueWorkItem(new WorkItemCallback(this.GetImageCallback), m_sFilePaths[i]);
                     }
                      //   GetImage(m_sFilePaths[i]);
                     //}
                 
                 //})).Start();
                
            }
            else
            {
                if (Directory.Exists(sFilePath))
                {
                    m_sFilePaths = new List<string>();

                    DirectoryInfo di = new DirectoryInfo(sFilePath);
                    string[] extensions = new[] { ".jpg", ".tiff", ".bmp" };

                    FileInfo[] files =
                        di.GetFiles()
                             .Where(f => extensions.Contains(f.Extension.ToLower()))
                             .ToArray();

                    //var files = di.GetFiles("*.jpg;*.png");
                    if (files != null)
                        foreach (var fi in files)
                        {
                            m_sFilePaths.Add(fi.FullName);
                        }


                }
                else
                {
                    LoadSlideShowFile(sFilePath);
                }
            }
        }