110,536
社区成员
发帖
与我相关
我的任务
分享
public partial class SplashFrm : Form
{
public SplashFrm()
{
InitializeComponent();
}
private string _StatusInfo = "";
//开始登陆前的加载信息
public string StatusInfo
{
set
{
_StatusInfo = value;
ChangeStatusText();
}
get
{
return _StatusInfo;
}
}
public void ChangeStatusText()
{
try
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(this.ChangeStatusText));
return;
}
this.lbStatus.Text = _StatusInfo;
}
catch (Exception e)
{
throw e;
}
}
}
public class Splash
{
//声明加载页
static SplashFrm MySplashForm = null;
//声明加载线程
static Thread MySplashThread = null;
static void ShowThread()
{
MySplashForm = new SplashFrm();
Application.Run(MySplashForm);
}
/// <summary>
/// 显示加载页
/// </summary>
static public void Show()
{
if (MySplashThread != null)
return;
MySplashThread = new Thread(new ThreadStart(Splash.ShowThread));
MySplashThread.IsBackground = true;
MySplashThread.SetApartmentState(ApartmentState.STA);
MySplashThread.Start();
}
/// <summary>
/// 关闭加载页
/// </summary>
static public void Close()
{
if (MySplashThread == null) return;
if (MySplashForm == null) return;
try
{
MySplashForm.Invoke(new MethodInvoker(MySplashForm.Close));
}
catch (Exception)
{
}
MySplashThread = null;
MySplashForm = null;
}
/// <summary>
/// 加载页状态
/// </summary>
static public string Status
{
set
{
if (MySplashForm == null)
{
return;
}
MySplashForm.StatusInfo = value;
}
get
{
if (MySplashForm == null)
{
throw new InvalidOperationException("Splash Form not on screen");
}
return MySplashForm.StatusInfo;
}
}
}
int intervalTime = 1200;
//显示加载页(新线程)
Login.Splash.Show();
Login.Splash.Status = "状态:正在加载配置文件。。。";
Thread.Sleep(intervalTime);
loadConfig();
Thread.Sleep(intervalTime);
Login.Splash.Status = "状态:正在测试连接数据库。。。";
Thread.Sleep(intervalTime);
ConnectDataBase();
Thread.Sleep(intervalTime);
//关闭加载页
Login.Splash.Close();
Form waitForm = null;
new Thread((ThreadStart)delegate
{
waitForm = new Form();
Application.Run(waitForm);
}).Start();
//执行你的操作
waitForm.Invoke((EventHandler)delegate { waitForm.Close(); });