上篇中寫了一個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 }
使用Splish的方法就和原來ProgressWindowForm的方法是一樣的,主要就是在主表單顯示前,添加如下的代碼:
this.Hide();
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();
}
}
}
代碼下載:/Files/zhjp11/ProgressWindowWithSplash.rar