关于Thread的实例
代码
///调用Wait类,并显示等待状态
private void button1_Click(object sender, EventArgs e)
{
////Wait类
Wait f = new Wait();
this.Hide();//隐藏主窗体
f.Show("加载数据中....");//显示等待窗体
Thread.Sleep(900);
f.Show("登录中....");
Thread.Sleep(900);
f.Show("加载中....");
Thread.Sleep(900);
f.Close();//关闭等待窗体
this.Show();//显示主窗体
}
////Wait类
using System;
using System.Threading;
using System.Text;
using System.Windows.Forms;
namespace WinFormWait
{
partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
/// <summary>
/// 设置显示信息
/// </summary>
/// <param name="message"></param>
public void SetMes(string message)
{
this.label1.Text = message;
}
}
public class Wait
{
/// <summary>
/// Thread对像
/// </summary>
private Thread thread = null;
/// <summary>
/// Form2对像
/// </summary>
private Form2 f = null;
/// <summary>
/// 默认构造函数
/// </summary>
public Wait()
{
///实例化
f = new Form2();
///创建线程并显示Form2窗体
thread = new Thread(new ThreadStart(delegate { f.ShowDialog(); }));
}
/// <summary>
/// 显示窗体
/// </summary>
/// <param name="message"></param>
public void Show(string message)
{
f.Show();//弹出窗体
f.SetMes(message);///设置信息
f.Refresh();//重新绘制窗体
}
/// <summary>
/// 关闭窗体并停止线程
/// </summary>
public void Close()
{
f.Close();
try
{ thread.Abort(); }
catch { }
}
}
}