实验4:线程管理练习
1、通过本实验,熟悉和掌握Thread类、ThreadPool类以及WPF中多线程的使用。
2、复习C#中lambda表达式和委托
1、创建一个WPF应用程序项目
2、将App.xaml中的Application.Resources节内容改为
3、修改MainWindow.xaml及代码隐藏类
MainWindow.xaml
MainWindow.xaml.cs主要内容
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;
namespace WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Button oldButton = new Button();
public MainWindow()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
Button btn = e.Source as Button;
btn.Foreground = Brushes.Black;
oldButton.Foreground = Brushes.Black;
oldButton = btn;
frame1.Source = new Uri(btn.Tag.ToString(), UriKind.Relative);
}
}
}
Page2.xaml
Page2.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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;
namespace WpfApp1.Examples
{
/// <summary>
/// Page2.xaml 的交互逻辑
/// </summary>
public partial class Page2 : Page
{
public Page2()
{
InitializeComponent();
Helps.ChangeState(btnStart, true, btnStop, false);
}
private void btnStop_Click(object sender, RoutedEventArgs e)
{
MyClass.IsSTop = true;
Helps.ChangeState(btnStart, true, btnStop, false);
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
Helps.ChangeState(btnStart, false, btnStop, true);
MyClass.IsSTop = false;
textBlock1.Text = "";
MyClass c = new MyClass(textBlock1);
MyData state = new MyData { Message = "a", Info = "\n线程1已终止" };
Thread thread1 = new Thread(c.MyMethod);
thread1.IsBackground = true;
thread1.Start(state);
state = new MyData { Message = "b", Info = "\n线程2已终止" };
Thread thread2 = new Thread(c.MyMethod);
thread2.IsBackground = true;
thread2.Start(state);
state = new MyData { Message = "c", Info = "\n线程3已终止" };
ThreadPool.QueueUserWorkItem(new WaitCallback(c.MyMethod), state);
state = new MyData { Message = "d", Info = "\n线程4已终止" };
ThreadPool.QueueUserWorkItem(new WaitCallback(c.MyMethod), state);
}
}
public class MyClass
{
public static volatile bool IsSTop;
TextBlock textBlock1;
public MyClass(TextBlock textBlock1)
{
this.textBlock1 = textBlock1;
}
public void MyMethod(object obj)
{
MyData state = obj as MyData;
while (IsSTop==false)
{
AddMessage(state.Message);
Thread.Sleep(1000);
}
AddMessage(state.Info);
}
private void AddMessage(string s)
{
textBlock1.Dispatcher.Invoke(() =>
{
textBlock1.Text += s;
});
}
}
public class MyData
{
public string Info { get; set; }
public string Message { get; set; }
}
}
新建Helps类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace WpfApp1.Examples
{
class Helps
{
public static void ChangeState(Button btnStart,bool isStart,Button btnStop,bool isStop)
{
btnStart.IsEnabled = isStart;
btnStop.IsEnabled = isStop;
}
}
}
运行结果
在实验3的基础上实现了实验4
通过本实验,即熟悉和掌握了线程管理Thread类、线程池ThreadPool类以及
WPF中多线程的使用,又复习C#中lambda表达式和委托。