Splash窗体(ProgressWindowForm修改)
上篇中写了一个ProgressWindowForm窗体,主要使用线程来处理系统中执行的大数据量操作的一个多线程解决方案,本次将其修改一下,使用线程去处理系统加载过程中的一些处理事项。
界面图如下:
这个Splash和原来的ProgressWindowForm的方式一样,只是修改了其中的少许代码,首先 就是本窗体没有了以前的取消方法,删除取消的相关代码就可以了,第二就是窗体不能被强制关闭,也就是说不能使用Alt+F4关闭本窗体,这是很简单的,我在窗体中添加了ProcessDialogKey来去除Alt+F4功能,代码如下:
1 protected override bool ProcessDialogKey(Keys keyData)
2 {
3 switch (keyData)
4 {
5 case Keys.Alt | Keys.F4:
6 return true;
7 default:
8 break;
9 }
10 return base.ProcessDialogKey(keyData);
11 }
2 {
3 switch (keyData)
4 {
5 case Keys.Alt | Keys.F4:
6 return true;
7 default:
8 break;
9 }
10 return base.ProcessDialogKey(keyData);
11 }
使用Splish的方法就和原来ProgressWindowForm的方法是一样的,主要就是在主窗体显示前,添加如下的代码:
this.Hide();
SplashWindowForm splashWindowForm = new SplashWindowForm();
splashWindowForm.Text = "测试工作";
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(SplashDoWork), splashWindowForm);
splashWindowForm.ShowDialog();
SplashWindowForm splashWindowForm = new SplashWindowForm();
splashWindowForm.Text = "测试工作";
System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(SplashDoWork), splashWindowForm);
splashWindowForm.ShowDialog();
SplashDoWork的代码如下:
private void SplashDoWork(object status)
{
SplashWindowForm splashWindowForm = status as SplashWindowForm;
try
{
splashWindowForm.BeginThread(0, 300);
for (int i = 0; i < 100; ++i)
{
splashWindowForm.SetDisplayText("加载启动项1.");
splashWindowForm.StepTo(i);
if (splashWindowForm.IsAborting)
{
return;
}
System.Threading.Thread.Sleep(100);
if (splashWindowForm.IsAborting)
{
return;
}
}
for (int i = 100; i < 200; ++i)
{
splashWindowForm.SetDisplayText("加载启动项2.");
splashWindowForm.StepTo(i);
if (splashWindowForm.IsAborting)
{
return;
}
System.Threading.Thread.Sleep(100);
if (splashWindowForm.IsAborting)
{
return;
}
}
for (int i = 200; i < 300; ++i)
{
splashWindowForm.SetDisplayText("加载启动项3.");
splashWindowForm.StepTo(i);
if (splashWindowForm.IsAborting)
{
return;
}
System.Threading.Thread.Sleep(100);
if (splashWindowForm.IsAborting)
{
return;
}
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message + Environment.NewLine + exception.StackTrace);
}
finally
{
if (splashWindowForm != null)
{
splashWindowForm.End();
}
}
}
{
SplashWindowForm splashWindowForm = status as SplashWindowForm;
try
{
splashWindowForm.BeginThread(0, 300);
for (int i = 0; i < 100; ++i)
{
splashWindowForm.SetDisplayText("加载启动项1.");
splashWindowForm.StepTo(i);
if (splashWindowForm.IsAborting)
{
return;
}
System.Threading.Thread.Sleep(100);
if (splashWindowForm.IsAborting)
{
return;
}
}
for (int i = 100; i < 200; ++i)
{
splashWindowForm.SetDisplayText("加载启动项2.");
splashWindowForm.StepTo(i);
if (splashWindowForm.IsAborting)
{
return;
}
System.Threading.Thread.Sleep(100);
if (splashWindowForm.IsAborting)
{
return;
}
}
for (int i = 200; i < 300; ++i)
{
splashWindowForm.SetDisplayText("加载启动项3.");
splashWindowForm.StepTo(i);
if (splashWindowForm.IsAborting)
{
return;
}
System.Threading.Thread.Sleep(100);
if (splashWindowForm.IsAborting)
{
return;
}
}
}
catch (Exception exception)
{
MessageBox.Show(exception.Message + Environment.NewLine + exception.StackTrace);
}
finally
{
if (splashWindowForm != null)
{
splashWindowForm.End();
}
}
}