|
|
|
C# WinApp Thread ซ้อน Thread แล้วค้างครับมีวิธีไม๊ครับ |
|
|
|
|
|
|
|
ผมทำการค้นหาไฟล์แต่ละโฟลเดอร์
แล้วแยก โฟลเดอร์ละ thead ไปทำงานปรากฏว่าเครื่องค้างครับ(คอมช้ามั้ง)
ช่วยแนะนำหน่อยครับว่าควรทำยังไงไม่จำเป็นต้องแยก โฟลเดอร์ละ thead ก็ได้ครับ
แค่ค้นหาไฟล์ แล้วลง datatable ให้เร็วขึ้นก็พอครับ
Code (C#)
DataTable dt;
private void button2_Click(object sender, EventArgs e)
{
dt = new DataTable();
dt.Columns.Add(new DataColumn("Artis_Song", typeof(string)));
dt.Columns.Add(new DataColumn("File", typeof(string)));
System.Threading.Thread thdAddLyric = new System.Threading.Thread(new System.Threading.ThreadStart(GetFiles)) { IsBackground = true };
thdAddLyric.Start();
}
string _dir;
private void AddSong()
{
foreach (string file in System.IO.Directory.GetFiles(_dir, "*.lyr"))
{
Random rnd = new Random();
string result = "" + rnd.Next(1, 99999) + rnd.Next(1, 99999) + rnd.Next(1, 99999) + rnd.Next(1, 99999) + rnd.Next(1, 99999) + rnd.Next(1, 99999) + rnd.Next(1, 99999) + rnd.Next(1, 99999);
try
{ result = (string)System.IO.File.ReadLines(file, System.Text.Encoding.GetEncoding(874)).Skip(1).Take(1).First() + " ; " + (string)System.IO.File.ReadLines(file, System.Text.Encoding.GetEncoding(874)).Skip(0).Take(1).First(); ;}
catch { }
try
{
DataRow dr = dt.NewRow();
dr["File"] = file;
dr["Artis_Song"] = result.Replace(" ", "").
Replace("(G)", "").Replace("(C)", "").
Replace("(Cm)", "").Replace("(C#)", "").Replace("(C#m)", "").
Replace("(D)", "").Replace("(Dm)", "").Replace("(E)", "").
Replace("(Eb)", "").Replace("(Ebm)", "").Replace("(Em)", "").
Replace("(F)", "").Replace("(Fm)", "").Replace("(F#)", "").
Replace("(F#m)", "").Replace("(G)", "").Replace("(Gb)", "").
Replace("(Gm)", "").Replace("(A)", "").Replace("(Ab)", "").
Replace("(Am)", "").Replace("(Abm)", "").Replace("(B)", "").
Replace("(Bb)", "").Replace("(Bm)", "").Replace("(Bbm)", "").ToLower().Trim();
dt.Rows.Add(dr);
}
catch { }
System.Threading.Thread.Sleep(50);
}
}
private void GetFiles()
{
Cursor = Cursors.WaitCursor;
System.Collections.Generic.Stack<string> stack = new System.Collections.Generic.Stack<string>();
stack.Push(PathData);
while ((stack.Count > 0))
{
string dir = stack.Pop();
_dir = dir;
System.Threading.Thread thd = new System.Threading.Thread(new System.Threading.ThreadStart(AddSong)) { IsBackground = true };
thd.Start();
string directoryName = null;
foreach (string directoryName_loopVariable in System.IO.Directory.GetDirectories(dir))
{
directoryName = directoryName_loopVariable;
stack.Push(directoryName);
}
}
dataGridView1.Invoke(new Action(() =>
{
dataGridView1.DataSource = dt;
dataGridView1.Columns[0].Width = 200;
dataGridView1.Columns[1].Width = 500;
dataGridView1.Sort(dataGridView1.Columns[0], ListSortDirection.Ascending);
}));
System.Threading.Thread.Sleep(50);
cri = "";
sngDel = 0;
t = 0;
string s_Lyr;
string s_Mid;
string s_Cur;
for (int i = 0; i < dataGridView1.RowCount - 1;i++ )
{
s_Lyr = dataGridView1[1, i].Value.ToString();
s_Mid = dataGridView1[1, i].Value.ToString().ToUpper().Replace("\\NCN\\LYRICS\\", "\\NCN\\SONG\\").Replace(".LYR", ".MID");
s_Cur = dataGridView1[1, i].Value.ToString().ToUpper().Replace("\\NCN\\LYRICS\\", "\\NCN\\CURSOR\\").Replace(".LYR", ".CUR");
if (dataGridView1[0, i].Value.ToString().Replace(" ", "").ToUpper() == cri ||!(System.IO.File.Exists(s_Cur)) ||!(System.IO.File.Exists(s_Mid)))
{
sngDel++;
DelFile(s_Lyr);
DelFile(s_Cur);
DelFile(s_Mid);
}
else
{ cri = dataGridView1[0, i].Value.ToString().Replace(" ", "").ToUpper(); }
label1.Invoke(new Action(() => { label1.Text = "Check:" + cri + " file:" + dataGridView1[1, i].Value.ToString(); }));
this.Invoke(new Action(() => { Text = " file:" + i + " Delete files:" + sngDel + " time:" + TimeSpan.FromSeconds((dataGridView1.RowCount - 1 - i) * t / i).ToString(@"hh\:mm\:ss"); }));
System.Threading.Thread.Sleep(50);
}
Cursor = Cursors.Default;
}
Tag : .NET, Win (Windows App), C#, VS 2012 (.NET 4.x), Windows
|
|
|
|
|
|
Date :
2014-12-03 15:36:33 |
By :
lamaka.tor |
View :
1122 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ไม่แระ
จำได้ว่าเคยแก้ method getfile ให้
แต่ทำไมโค้ดยังเหมือนเดิม
ให้ไปใหม่ก็คงไม่เอาไปใช้อยู่ดี เสียเวลา
|
|
|
|
|
Date :
2014-12-03 16:51:45 |
By :
ห้ามตอบเกินวันละ 2 กระทู้ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|