Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > C# สอบถาม เรื่องการ เพิ่ม ค่าใน List ขณะที่มีการเรียกใช้ ครับ



 

C# สอบถาม เรื่องการ เพิ่ม ค่าใน List ขณะที่มีการเรียกใช้ ครับ

 



Topic : 134832



โพสกระทู้ ( 4,436 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook



1

2


คือผม อยากจะ ใช้ downloaderProgress1.Add(.....) เพื่อเพิ่ม ข้อมูลลงใน List ของ downloaderProgress1
จากนั้นก็ สั่ง downloaderProgress1.Start(); เพื่อ รันงาน

แล้วผมอยาก เพิ่ม ข้อมูลลงใน List ของ downloaderProgress1 อีก เรื่อยๆ ครับ

คำถามคือ

จะบอก downloaderProgress1 ยังไงว่า LinkLoad.Count มันเพิ่มขึ้นมา
หรือว่า มี ข้อมูลเพิ่มขึ้นมาแล้วนะ

เช่น จากแต่ก่อน มันมี อยู่ 500 ข้อมูล เราสั่ง Start
ขณะที่รันอยู่ก็เพิ่มไปอีก 300 ข้อมูล อยากให้มันรู้ว่า ข้อมูลมันมี 800 นะ ไม่ใช่ 500 เหมือนเดิมแล้ว

โค้ด ครับ

Code (C#)
namespace Downloader
{
    public class FileLink
    {
        public string FileTarget;
        public string Link;
        public FileLink(string lnk, string f)
        {
            FileTarget = f;Link = lnk;
        }
    }
    public class DownloaderProgress : ProgressBar
    {
        List<FileLink> LinkLoad;
        public long BytesReceived = 0;
        public long TotalBytesToReceive = 0;
        public DownloaderProgress() : base()
        {
            LinkLoad = new List<FileLink>();
            SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);

        }
        #region _EventHandler
       

        public event EventHandler DownloadDataAllCompleted;
        protected virtual void OnDownloadDataAllCompleted(EventArgs e)
        {
            if (DownloadDataAllCompleted != null) DownloadDataAllCompleted(this, e);
        }


        public event DownloaderFileHandler DownloadDataCompleted;
        protected virtual void OnDownloadDataCompleted(DownloaderFileEventArgs e)
        {
            if (DownloadDataCompleted != null) DownloadDataCompleted(this, e);
        }

        public event DownloaderFileHandler DownloadProgressChanged;
        protected virtual void OnDownloadProgressChanged(DownloaderFileEventArgs e)
        {
            if (DownloadProgressChanged != null) DownloadProgressChanged(this, e);

        }
        public event DownloaderFileHandler DownloadError;
        protected virtual void OnDownloadError(DownloaderFileEventArgs e)
        {
            if (DownloadError != null) DownloadError(this, e);

        }
        public event DownloaderFileHandler DownloadFile;
        protected virtual void OnDownloadFile(DownloaderFileEventArgs e)
        {
            if (DownloadFile != null) DownloadFile(this, e);

        }



        public event DownloaderProgressChangedHandler DownloadStatus;
        protected virtual void OnDownloadStatus(DownloaderProgressChangedEventArgs e)
        {
            if (DownloadStatus != null) DownloadStatus(this, e);

        }


        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            // Clear the background.
            e.Graphics.Clear(this.BackColor);
            // Draw the progress bar.
            float fraction = (float)(this.Value - this.Minimum) / (this.Maximum - this.Minimum);

            ProgressBarRenderer.DrawHorizontalBar(e.Graphics, new Rectangle(new Point(0, 0), this.Size));
            ProgressBarRenderer.DrawHorizontalChunks(e.Graphics, new Rectangle(new Point(1, 1),
             new Size((int)(((this.Size.Width - 2) / 100.0) * fraction), this.Size.Height - 2)));

            int wid = (int)(fraction * this.ClientSize.Width);
            e.Graphics.FillRectangle(
                Brushes.LightGreen, 0, 0, wid,
                this.ClientSize.Height);

            // Draw the text.
            e.Graphics.TextRenderingHint =
                TextRenderingHint.AntiAliasGridFit;


            using (StringFormat sf = new StringFormat())
            {
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;

                if (Value == Maximum)
                {
                    e.Graphics.DrawString("Complete...", this.Font, Brushes.Black, this.ClientRectangle, sf);
                }
                else
                {
                    e.Graphics.DrawString((fraction).ToString("0.000 %") + " : " + Value + "/" + Maximum, this.Font, Brushes.Black, this.ClientRectangle, sf);
                }

            }

        }

        #endregion



        public void Add(FileLink lnk)
        {
            LinkLoad.Add(lnk);
            this.Invoke(new Action(() => { try { this.Maximum++; } catch { } }));
        }
        public void Add(string lnk,string Fileload)
        {
            LinkLoad.Add(new FileLink(lnk,Fileload));
            this.Invoke(new Action(() => { try { this.Maximum++; } catch { } }));
        }
        public void AddRange(List<FileLink> lnks)
        {
            LinkLoad.AddRange(lnks);
            this.Invoke(new Action(() => { try { this.Maximum+= LinkLoad.Count; } catch { } }));
        }
        public void Start()
        {
            
            if (LinkLoad == null || LinkLoad.Count <= 0)
            {

                OnDownloadDataAllCompleted(new EventArgs());
                return;
            }
            int complete = 0;
            LinkLoad
                .ForEach(l =>
                {
                    
                        OnDownloadStatus(new DownloaderProgressChangedEventArgs(l.Link, "Downloading...."));
                    string f;
                    if (File.Exists(l.FileTarget))
                    {
                        f = RenameFileDup(l.FileTarget);
                    }
                    else
                    {
                        f = l.FileTarget;
                    }

                    if (!Directory.Exists(Path.GetDirectoryName(l.FileTarget)))
                        Directory.CreateDirectory(l.FileTarget);
                    using (WebClient wc = new WebClient())
                    {
                        wc.DownloadFileCompleted += new AsyncCompletedEventHandler((s, e) =>
                        {
                            this.Invoke(new Action(() => { try { this.Value++; } catch { } }));
                            complete++;
                            OnDownloadStatus(new DownloaderProgressChangedEventArgs(l.Link, "Complete.."));
                            OnDownloadDataCompleted(new DownloaderFileEventArgs(l.Link, f));
                            if (complete >= LinkLoad.Count)
                            {
                                return;

                            }

                        });
                        wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler((s, e) =>
                        {

                            this.Invoke(new Action(() => { this.BytesReceived += (int)e.BytesReceived; }));
                            this.Invoke(new Action(() => { this.TotalBytesToReceive += (int)e.TotalBytesToReceive; }));
                        });
                        try
                        {
                            OnDownloadFile(new DownloaderFileEventArgs(l.Link, f));
                            wc.DownloadFile(new Uri(l.Link), f);
                        }
                        catch (Exception ex)
                        {
                            OnDownloadError(new DownloaderFileEventArgs(l.Link, f + ":" + ex.ToString()));

                        }
                    }
                });


        }


        string RenameFileDup(string _File)
        {
            string stg = _File;
            if (System.IO.File.Exists(_File))
            {
                int i = 1;
                do
                {
                    stg = System.IO.Path.GetDirectoryName(_File) + "\\" + System.IO.Path.GetFileNameWithoutExtension(_File) + "_" + i + System.IO.Path.GetExtension(_File);
                    i++;
                } while (System.IO.File.Exists(stg));
            }
            return stg;
        }

    }
}




Code (C#)
       private void Form1_Load(object sender, EventArgs e)
        {

            
            new System.Threading.Thread(() =>
            {

                new List<string>() { "https://wallpaperplay.com/genres/abstract",
                                    "https://wallpaperplay.com/genres/animals",
                                    "https://wallpaperplay.com/genres/anime",
                                    "https://wallpaperplay.com/genres/art",
                                    "https://wallpaperplay.com/genres/cars",
                                    "https://wallpaperplay.com/genres/celebreties",
                                    "https://wallpaperplay.com/genres/city",
                                    "https://wallpaperplay.com/genres/comics",
                                    "https://wallpaperplay.com/genres/flowers",
                                    "https://wallpaperplay.com/genres/games",
                                    "https://wallpaperplay.com/genres/girls",
                                    "https://wallpaperplay.com/genres/holidays",
                                    "https://wallpaperplay.com/genres/horror",
                                    "https://wallpaperplay.com/genres/love",
                                    "https://wallpaperplay.com/genres/men",
                                    "https://wallpaperplay.com/genres/movies",
                                    "https://wallpaperplay.com/genres/music",
                                    "https://wallpaperplay.com/genres/nature",
                                    "https://wallpaperplay.com/genres/other",
                                    "https://wallpaperplay.com/genres/sci-fi",
                                    "https://wallpaperplay.com/genres/space",
                                    "https://wallpaperplay.com/genres/sport",
                                    "https://wallpaperplay.com/genres/textures",
                                    "https://wallpaperplay.com/genres/tv-series",

                }.ForEach(_url =>
                {
                    string fol = Path.GetFileName(_url);
                  
                        _url.GetLinkByURL(@" target=""_blank""" + "\n" + @"               href=""(https://wallpaperplay.com/board/.*?)"">")
                        .ForEach(url =>
                        {

                            url.GetLinkByURL(@"data-src=""(/walls/.*?/.*?.jpg)""")
                              .ForEach(l =>
                              {
                                  downloaderProgress1.Invoke(new Action(() => downloaderProgress1.Add("https://wallpaperplay.com" + l, @"G:\wallpaperplay\" + fol + "\\" + Path.GetFileName(l))));
                                 // TorServices.Net.NetWorkTOR.LoadFByWebClient("https://wallpaperplay.com" + l, @"G:\wallpaperplay\" +fol + "\\" + Path.GetFileName(l));
                              });

                          /*  Invoke(new MethodInvoker(() =>
                            {
                                this.Text = url;
                              
                               richTextBox1.Text += "https://wallpaperplay.com" + string.Join("\nhttps://wallpaperplay.com", url.GetLinkByURL(@"data-src=""(/walls/.*?/.*?.jpg)""")) + "\n";
                            }));*/


                        });
                    downloaderProgress1.Invoke(new Action(() => downloaderProgress1.Start()));
                });

               // downloaderProgress1.Invoke(new Action(() => downloaderProgress1.Start()));
                this.Invoke(new Action(() => this.Text = "Complete..."));
            }).Start();
        }




Tag : .NET, C#, VS 2015 (.NET 4.x)







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2020-01-31 17:07:21 By : lamaka.tor View : 1052 Reply : 4
 

 

No. 1



โพสกระทู้ ( 9,559 )
บทความ ( 2 )



สถานะออฟไลน์


ลักษณะการทำงานเป็น dynamic อย่าใช้ for
ให้ใช้ do while แทนครับ มันจะได้ตรวจสอบ ค่า ทุกครั้ง ค่าอาจจะเพิ่ม หรือ จะ ลด จาก method ไหน ตอนไหนก็ได้
public x = 1000;
do{
.....
}while (x>0);






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2020-01-31 20:57:16 By : Chaidhanan
 


 

No. 2



โพสกระทู้ ( 4,436 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ตอบความคิดเห็นที่ : 1 เขียนโดย : Chaidhanan เมื่อวันที่ 2020-01-31 20:57:16
รายละเอียดของการตอบ ::
เดี๋ยวขอลองดูก่อนนะครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2020-01-31 21:59:45 By : lamaka.tor
 

 

No. 3



โพสกระทู้ ( 1,458 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter Facebook Blogger

WhenAny()/WaitAny() + Continue​With() ไม่ได้เหรอครับ

https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task?view=netframework-4.8
https://gist.github.com/ehsan-davoudi/8688427
https://stackoverflow.com/questions/17021701/how-to-determine-when-all-task-is-completed


หรือ ConcurrentQueue ดีไหม?
https://docs.microsoft.com/en-us/dotnet/api/system.collections.concurrent.concurrentqueue-1?view=netframework-4.8
https://stackoverflow.com/questions/31485375/waitall-for-changing-listtask


ประวัติการแก้ไข
2020-02-03 20:34:45
2020-02-03 20:35:22
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2020-02-03 20:23:26 By : PhrayaDev
 


 

No. 4



โพสกระทู้ ( 1,458 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter Facebook Blogger

เห็นถามหา DM จากกระทู้ล่าง

เห็นชัดชัด https://github.com/erickutcher/httpdownloader
งูใหญ่ https://github.com/persepolisdm/persepolis
จุดตาข่าย ยังไม่เจอที่ถูกใจ (ลองดู SGet เป็น WPF หรือ Universal Downloader ของ Laiba Bukhari)
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2020-02-03 20:33:28 By : PhrayaDev
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : C# สอบถาม เรื่องการ เพิ่ม ค่าใน List ขณะที่มีการเรียกใช้ ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่