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();
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)