Delegate ใน C# (2010-03-16)
Delegate คือ อะไร เอาแบบง่ายๆ ก็คือ Delegate เป็น Reference Type ของ Method ไม่ว่าจะเป็น Static Method หรือว่าเป็น
Method ของ Object ใดๆ ลองมาดูตัวอย่างง่ายๆ กันนะครับ
delegate มีความจำเป็นในหลายๆ สถานการณ์ เช่น
* multi-thread: ในภาษา C# เราสามารถวิ่งหลายๆ โปรเซสไปพร้อมๆ กันได้เรียกว่า thread เมื่อจะเริ่ม thread ใหม่เราจำเป็นต้องระบุ method ที่จะให้ไปทำงานโดยอาศัย delegate
* Library: เมื่อสร้างคลาสไลบรารีบ่อยครั้งที่คลาสจำเป็นต้องเรียกกลับไปยังคลาสผู้ขอ ใช้บริการ (class client) ซึ่งทำได้โดยอาศัย delegate เท่านั้น
* Event: โปรแกรมที่ทำงานบนระบบปฏิบัติการ Windows จะพบ event ตลอดเวลา (event-driven programming) เช่นเมื่อผู้ใช้คลิกเมาส์จะมี event Mouse_Click เกิดขึ้น การกำหนดว่าจะให้ไปทำงาน method ใดเมื่อเกิด event จำเป็นต้องอาศัย delegate
(ผมก็ยอมรับว่ายังงง ๆ ที่จะนำไปใช้งาน ยังไม่เคยเอาไปใช้งานจริงๆ ซะที คงต้องมีสักวันเจอกัน Delegate)
1.Delegate ของ Static Method ธรรมด
|
//ประกาศ ตัวแทนชื่อ MyDelegate ที่จะทำงานแทน Method ที่ไม่มีการคืนค่าและรับ parameter 1 ตัวมีชนิดเป็น String
public delegate void MyDelegate(String str);
public void ShowMyName(String name)
{
MessageBox.Show("My name is "+name);
}//
//ประกาศตัวแทนของ MyDelegate ชื่อว่า CallDelegate โดยทำหน้าที่แทน Method ที่ชื่อว่า ShowMyName
MyDelegate CallDelegate = new MyDelegate(ShowMyName);
CallDelegate("Chiwa");
ผลก็จะมี Message Box ขึ้นโชว์ว่า My name is Chiwa
|
2.Delegate ของ Method ใน Object ต่าง ๆ
|
class SampleClass
{
public void Call1(String str)
{
MessageBox.Show("Call1 : " + str);
}//
public void Call2(String str)
{
MessageBox.Show("Call2 : " + str);
}//
}
//========================================================//
//ประกาศ ตัวแทนชื่อ MyDelegate ที่จะทำงานแทน Method ที่ไม่มีการคืนค่าและรับ parameter 1 ตัวมีชนิดเป็น String
public delegate void MyDelegate(String str);
MyDelegate CallD = null;
//เราสามารถเพิ่ม method เข้าไปใน delegate ได้ โดยใช้สัญลักษณ์ += เรียกว่าการทำ multicast ทำได้หลายงานว่ากันไป แต่ signature แต่ละ method ก็ต้องเป็น singature เดียวกัน
CallD += new MyDelegate(new SampleClass().Call1);
CallD += new MyDelegate(new SampleClass().Call2);
CallD("Hello1");
จากโค้ดด้านบน จะเป็นการ ผูก Method ให้กับ CallD ไปสองตัวคือ Call1 และ Call2
ผลการรันจะได้ Message Box สองตัวคคือ
1. Call1 : Hello1
2. Call2 : Hello1
|
3.Delegate กับการประยุกต์ใช้การส่งค่าและรับค่าจาก Thread
|
public String Run(String name)
{
return "Your Are "+name;
}
//===========================================================//
String getName = "";
Thread MyThread = new Thread(delegate()
{
getName = Run("Chiwa Kantawong ");
});
MyThread.Start();
while (MyThread.IsAlive)
Thread.Sleep(1);
MessageBox.Show(getName);
|
4. ประยุกต์ delegate ควบคุม windows control ใน Thread
|
จะควบคุม Windows Control ใน Thread ได้อย่างไร (2010-02-04)
ตามตัวอย่างเลยนะครับ ไม่ยากมากเข้าใจง่ายครับ
delegate void SetTextCallback(string text);
private void setText(String text)
{
if (this.textBox1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(setText);
this.Invoke(d, new object[] { text });
}
else
{
this.textBox1.Text = text;
}
}
private void Run2()
{
int i = 0;
while (i <= 100)
{
this.setText("Number ==> "+i);
i++;
Thread.Sleep(2000);
}
}
|
ทำความเข้าใจได้ไม่ยากใช่ไหมครับ
|