|
|
|
C# WinApp task[] ให้ค่าซ้ำกัน ครับ แต่ถ้าใช้ thread ไม่มีปัญหาครับ |
|
|
|
|
|
|
|
Code (C#)
new System.Threading.Thread(getListHTMLByTask).Start();
Code (C#)
void getListHTMLByTask()
{
//เก็บเวลาเริ่มต้นทำงาน
DateTime s = DateTime.Now;
string strsearch = txtS.Text;
int _min = int.Parse("0" + txtMin.Text);
int _max = int.Parse("0" + txtMax.Text);
var tasks = new List<Task>(); // สร้าง task มาใช้งานซะ
for (int i = _min; i <= _max; i++)
{
tasks.Add( Task.Run(() =>
{
// เรียกใช้ delecate getListHTMLByTask
// getList(new object[] { "http://www.one2up.com/list_content.php?page=" + i + "&txt_Search=" + strsearch + "&radio_type=content_Name"});//member_displayname
getList(new object[] { "http://www.one2up.com/list_content.php?page=" + i + "&txt_Search=" + strsearch + "&radio_type=member_displayname"});//member_displayname
}));
}
try
{
//ขณะรอก็จิบ 40 degree ไปก่อน
Task.WaitAll(tasks.ToArray());
}
catch { }
//เก็บเวลาสิ้นสุดการทำงาน
DateTime ss = DateTime.Now;
TimeSpan ts = ss - s;
//ประมวลเวลาทั้งหมด
this.Invoke(new Action(() => { this.Text = "run complete.. Time:" + ts.Hours + ":" + ts.Minutes + ":" + ts.Seconds + " " + ts.Milliseconds; }));
}
void getListHTMLByThread()
{
string strsearch = txtS.Text;
int _min = int.Parse("0" + txtMin.Text);
int _max = int.Parse("0" + txtMax.Text);
for (int i = _min; i <= _max; i++)
{
new System.Threading.Thread(getList).Start(new object[] { "http://www.one2up.com/list_content.php?page=" + i + "&txt_Search=" + strsearch + "&radio_type=content_Name" });//member_displayname
try
{ this.Invoke(new Action(() => { Text = "URL:" + "http://www.one2up.com/list_content.php?page=" + i + "&txt_Search=" + strsearch + "&radio_type=content_Name"; })); }
catch { }
new System.Threading.Thread(getList).Start(new object[] { "http://www.one2up.com/list_content.php?page=" + i + "&txt_Search=" + strsearch + "&radio_type=member_displayname" });//member_displayname
try
{ this.Invoke(new Action(() => { Text = "URL:" + "http://www.one2up.com/list_content.php?page=" + i + "&txt_Search=" + strsearch + "&radio_type=member_displayname"; })); }
catch { }
System.Threading.Thread.Sleep(100);
}
}
void getList(object obj)
{
string URL = (string)((object[])obj)[0];
string _HTML = clsHTML.getHTML(URL);
string strID = "";
System.Text.RegularExpressions.Regex.Matches(_HTML, @"(?:<div.*?(view_content.php\?content_ID=[0-9]+)\"".*?<b>(.*?)<.*?ขนาดไฟล์(.*?)<br>)")
.OfType<System.Text.RegularExpressions.Match>().ToList<System.Text.RegularExpressions.Match>()
.ForEach(match =>
{
try
{
strID = new System.Text.RegularExpressions.Regex(@"""txt_URL"" id=""txt_URL"" value=[\""|']?(.*?)[\""|'|>]", System.Text.RegularExpressions.RegexOptions.Singleline | System.Text.RegularExpressions.RegexOptions.CultureInvariant).Matches(clsHTML.getHTML("http://www.one2up.com/" + match.Groups[1].Value))[0].Groups[1].Value;//regexDate.Matches(strID)[0].Groups[1].Value
System.Threading.Thread.Sleep(200);
dataGridView1.Invoke(new Action(() => {
dataGridView1.Rows.Add("http://www.one2up.com/" + match.Groups[1].Value, match.Groups[2].Value, match.Groups[3].Value, strID, "");
dataGridView1.Refresh();
}));
}
catch { }
});
try
{ this.Invoke(new Action(() => { Text = "Set Link Complete...."; })); }
catch { }
}
ถ้าใช้ getListHTMLByThread ทำงานปกติครับ แต่ถ้า getListHTMLByTask จะได้ค่าซ้ำกันออกมาครับ
Tag : .NET, Win (Windows App), C#, VS 2012 (.NET 4.x), VS 2013 (.NET 4.x), Windows
|
|
|
|
|
|
Date :
2017-08-23 00:30:29 |
By :
lamaka.tor |
View :
1337 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|