ผมอยากเขียนโปรแกรมที่
1.ทำงานได้หลายๆ thead
2.ขณะที่ทำงานให้แจ้งผลผ่าน display เช่น textbox,richTextBox แบบไม่กระตุก
3. หลังทำงานเสร็จแล้ว ประมวลเวลาทั้งหมดที่ทำงาน
ตอนนี้ที่ทดสอบคร่าวๆประมาณนี้ครับ
[Spoil] คลิกเพื่อดูข้อความที่ซ่อนไว้
DateTime dt;
void Run_1(object obj)
{
string _s = (string)((object[])obj)[0];
int _min = (int)((object[])obj)[1];
int _max = (int)((object[])obj)[2];
for (int i = _min; i <= _max; i++)
{
// this.workerProgressbar1.ReportProgress((double)i-_min * 100 / 10000);
this.Invoke(new Action(() => { this.Text = "" + i; }));
System.Threading.Thread.Sleep(100);
}
}
private void button1_Click(object sender, EventArgs e)
{
// workerProgressbar1.DoWork += backgroundWorker1_DoWork;
workerProgressbar1.RunWorkerAsync();
// new System.Threading.Thread(new System.Threading.ThreadStart(Run_1)).Start();
// workerProgressbar1.RunWorkerAsync(Run_1(new object[]{"asf",1,500}));
// new System.Threading.Thread(new System.Threading.ThreadStart(Run_1)).Start();
}
private void backgroundWorker1_DoWork_1(object sender, DoWorkEventArgs e)
{
for (int i = 1; i < 10; i++)
Run_2();
}
void Run_2()
{
int max = new Random().Next(10, 100);
for (int i = 1; i <max; i++)
{
this.richTextBox1.Invoke(new Action(() => { this.richTextBox1.Text += "\n max:" +max +" tor " + i; }));
System.Threading.Thread.Sleep(100);
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.Invoke(new Action(() => { this.Text = "time:"+ (DateTime.Now-dt).ToString(@"hh\:mm\:ss"); }));
}
//ทดสอบ backgroundWorker
private void button2_Click(object sender, EventArgs e)
{
this.richTextBox1.Invoke(new Action(() => { this.richTextBox1.Text += ""; }));
dt = DateTime.Now;
backgroundWorker1.RunWorkerAsync();
}
//ทดสอบ Task
private void button3_Click(object sender, EventArgs e)
{
this.richTextBox1.Invoke(new Action(() => { this.richTextBox1.Text += ""; }));
dt = DateTime.Now;
var list = new System.Collections.Concurrent.ConcurrentBag<string>
);
string[] dirNames = System.IO.Directory.GetDirectories(@"I:\TestCSharp", "*.*", System.IO.SearchOption.TopDirectoryOnly);
List<Task> tasks = new List<Task>
);
foreach (var dirName in dirNames)
{
Task t = new Task(() =>
{
foreach (var path in System.IO.Directory.GetFiles(dirName, "*.*", System.IO.SearchOption.TopDirectoryOnly))
{ list.Add(path); this.Invoke(new Action(() => { this.Text = "Path:" + path; })); }
});
tasks.Add(t);
t.Start();
}
Task.WaitAll(tasks.ToArray());
foreach (Task t in tasks)
richTextBox1.Invoke(new Action(() => { richTextBox1.Text += "\n " + string.Format(" Task {0} Status: {1}", t.Id, t.Status); }));
richTextBox1.Invoke(new Action(() => { richTextBox1.Text += "\n " + string.Format("Number of files read: {0}", list.Count); }));
this.Invoke(new Action(() => { this.Text = "time:" + (DateTime.Now - dt).ToString(@"hh\:mm\:ss"); }));
}
//ทดสอบ Thread
private void button4_Click(object sender, EventArgs e)
{
this.richTextBox1.Invoke(new Action(() => { this.richTextBox1.Text += ""; }));
dt = DateTime.Now;
for (int n = 0; n <= 10; n++)
new System.Threading.Thread(new System.Threading.ThreadStart(Run_2)).Start();
this.Invoke(new Action(() => { this.Text = "time:" + (DateTime.Now - dt).ToString(@"hh\:mm\:ss"); }));
}
จากโค้ดที่ผมเขียน ในส่วนของ Threading สามารถทำงานได้ทีละหลายๆ thread ได้ก็จริงแต่ ไม่สามารถประมวลผลเวลาโดยรวมได้
ส่วน backgroundWorker และ task สามารถประมวลผลเวลาได้ก็จริง แต่ก็ยังไม่เป็น mutithread ทั้งหมดยังคงทำงานภายใต้ 1 thread ถึงแม้จะแยกกันทำงานทีละขั้นตอนก็ตาม
C# อยากทราบว่า backgroundWorker Task Threading ใช้ร่วมกันยังไงดีรึครับ
1.ทำงานได้หลายๆ thead
2.ขณะที่ทำงานให้แจ้งผลผ่าน display เช่น textbox,richTextBox แบบไม่กระตุก
3. หลังทำงานเสร็จแล้ว ประมวลเวลาทั้งหมดที่ทำงาน
ตอนนี้ที่ทดสอบคร่าวๆประมาณนี้ครับ
จากโค้ดที่ผมเขียน ในส่วนของ Threading สามารถทำงานได้ทีละหลายๆ thread ได้ก็จริงแต่ ไม่สามารถประมวลผลเวลาโดยรวมได้
ส่วน backgroundWorker และ task สามารถประมวลผลเวลาได้ก็จริง แต่ก็ยังไม่เป็น mutithread ทั้งหมดยังคงทำงานภายใต้ 1 thread ถึงแม้จะแยกกันทำงานทีละขั้นตอนก็ตาม