|
|
|
C# สอบถาม เรื่องการ เพิ่ม ค่าใน List ขณะที่มีการเรียกใช้ ครับ |
|
|
|
|
|
|
|
คือผม อยากจะ ใช้ 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)
|
|
|
|
|
|
Date :
2020-01-31 17:07:21 |
By :
lamaka.tor |
View :
1142 |
Reply :
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ลักษณะการทำงานเป็น dynamic อย่าใช้ for
ให้ใช้ do while แทนครับ มันจะได้ตรวจสอบ ค่า ทุกครั้ง ค่าอาจจะเพิ่ม หรือ จะ ลด จาก method ไหน ตอนไหนก็ได้
public x = 1000;
do{
.....
}while (x>0);
|
|
|
|
|
Date :
2020-01-31 20:57:16 |
By :
Chaidhanan |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|