1、 定义并使用委托实现如下输出:
Morning, Jimmy Zhang
早上好, 张子阳
2、 分别通过以下三种方式将信息添加到窗口中。
3、如何在页面不停的显示字母“a”?
4、如何在页面不停的显示字母“a”和“b”?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
namespace ch003
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//第一题
private void button1_Click(object sender, RoutedEventArgs e)
{
textBlock1.Text = "第一题(定义并使用委托)\n";
Great("Jimmy Zhang", EnglishGreat);
Great("张子阳",ChineseGreat);
}
public void ChineseGreat(string name)
{
textBlock1.Text += "早上好," + name+"\n";
}
public void EnglishGreat(string name)
{
textBlock1.Text += "Morning," + name+"\n";
}
public delegate void MakeGreating(string name);
private void Great(string name,MakeGreating mg)
{
mg(name);
}
//第二题
private void button2_Click(object sender, RoutedEventArgs e)
{
textBlock1.Text = "第二题(三种方式显示‘你好’)\n";
//2.第一种
textBlock1.Text += "你好\n";
//2.第二种
Add1();
//2.第三种
Thread t1 = new Thread(AddMessage);
t1.Start();
}
private void Add1()
{
textBlock1.Text += "你好\n";
}
public delegate void AddMessageDelegate();
private void AddMessage()
{
AddMessageDelegate d = Add2;
textBlock1.Dispatcher.Invoke(d);
}
private void Add2()
{
textBlock1.Text += "你好";
}
//第三题
private void button3_Click(object sender, RoutedEventArgs e)
{
textBlock1.Text = "第三题(在页面不停的显示字母a)\n";
Thread t2 = new Thread(MyShowa);
t2.Start();
}
public delegate void show();
private void MyShowa()
{
show d = Show_a;
for (int i = 1; i <= 100;i++ )
{
textBlock1.Dispatcher.Invoke(d);
Thread.Sleep(100);
}
}
private void Show_a()
{
textBlock1.Text += "a ";
}
//第四题
private void button4_Click(object sender, RoutedEventArgs e)
{
textBlock1.Text = "第四题(在页面不停的显示字母a和b)\n";
Thread t3 = new Thread(MyShowa);
t3.Start();
Thread t4 = new Thread(MyShowb);
t4.Start();
}
private void MyShowb()
{
show d = Show_b;
for (int i = 1; i <= 100; i++)
{
textBlock1.Dispatcher.Invoke(d);
Thread.Sleep(100);
}
}
private void Show_b()
{
textBlock1.Text += "b ";
}
}
}
在屏幕上不断地输出abcd并且可以停止
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
namespace ch03
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private static volatile bool isstop = false;
private void button1_Click(object sender, RoutedEventArgs e)
{
isstop = false;
Data data1 = new Data { Message = "a", Info = "\n线程1已停止" };
Thread t1 = new Thread(Mymethod);
t1.Start(data1);
Data data2 = new Data { Message = "b", Info = "\n线程2已停止" };
Thread t2 = new Thread(Mymethod);
t2.Start(data2);
//使用线程池
Data data3 = new Data { Message = "c", Info = "\n线程3已停止" };
ThreadPool.QueueUserWorkItem(new WaitCallback(Mymethod),data3);
Data data4 = new Data { Message = "d", Info = "\n线程4已停止" };
ThreadPool.QueueUserWorkItem(new WaitCallback(Mymethod), data4);
}
public void Mymethod(object m)
{
Data obj = m as Data;//类型转换
while (isstop==false)
{
method(obj.Message);
Thread.Sleep(100);
}
method(obj.Info);//输出结束语
}
public void method(string m)
{
//委托 vs2010写法
Action act = delegate()
{
textBlock1.Text += m;
};
textBlock1.Dispatcher.Invoke(act);
}
private void button2_Click(object sender, RoutedEventArgs e)
{
isstop = true;
}
}
public class Data
{
public string Info { get; set; }
public string Message { get; set; }
}
}