using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TestApplaction
{
//委托观察者模式
public partial class Form1 : Form
{
private TestDe test;
public Form1()
{
InitializeComponent();
test = new TestDe();
test.showEvent += setText;
}
private void button1_Click(object sender, EventArgs e)
{
test.start();
}
public void setText(String message)
{
textBox1.Text = message;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace TestApplaction
{
public class TestDe
{
//委托
public delegate void ShowMessge(String messge);
//事件
public event ShowMessge showEvent;
public void sendMessage(String str)
{
showEvent(str);
}
public void start()
{
Thread.Sleep(3000);
sendMessage("3秒后收到了消息");
}
}
}