c# - 在 C# 中显示启动画面的异常
问题描述
背景:几个月前,我得到了一个自由职业者编写的桌面应用程序的代码。此后,这位自由职业者关闭了他的账户。在过去的几天里,我一直在努力学习和破译已经编写的代码。我基本上是一个 C# 新手,并且正在用 Udemy C# 课程自学。
问题:当应用程序启动时,它显示一个启动画面。我可以在启动屏幕正确显示的发布模式下运行应用程序。但是在设置中勾选了公共语言运行时异常的调试模式下,我得到了一个异常。详情如下。
System.Threading.ThreadAbortException occurred
HResult=-2146233040
Message=Thread was being aborted.
StackTrace:
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at CamCon.MainForm.StartSplash() in E:\Freelance\CamArd\App_C# (2020.03.04)\CamCon\MainForm.cs:line 289
InnerException:
这些是相关的代码片段:
public MainForm()
{
InitializeComponent();
//show splash screen for 2.5seconds
Thread t = new Thread(new ThreadStart(StartSplash));
t.Start();
Thread.Sleep(2500);
t.Abort();
}
和:
private void StartSplash()
{
Application.Run(new SplashForm()); // Exception happens here. This is MainForm.cs:line 289
}
和:
namespace CameraControl
{
partial class SplashForm
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Image = global::CameraControl.Properties.Resources.splash;
this.pictureBox1.Location = new System.Drawing.Point(0, 0);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(600, 400);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// SplashForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(600, 400);
this.ControlBox = false;
this.Controls.Add(this.pictureBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "SplashForm";
this.ShowIcon = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "SplashForm";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
}
}
请求:任何人都可以帮助并建议我应该看什么,以纠正这个异常;这样它就不会在应用程序的其他地方回来咬我(而且我在其他地方也遇到了一些问题,但那是以后再说)?
提前谢谢了!
解决方案
SplashScreen
.NET 有一个可以使用的内置类。文档可以在这里找到。
关于例外,这是设计使然。您正在调用Thread.Abort
,并且通过逐字抛出一个ThreadAbortException
. 不是什么好事。
Thread.Abort
由于各种原因,我不会详细说明,在几乎所有情况下,使用都是一个糟糕的设计。但从表面上看,您可以看到简单地抛出异常并不是停止线程的干净方法。
推荐阅读
- angular - 从另一个目录调用 angular build
- bash - 如何从 git 挂钩中可靠地找到根 git 目录?
- regex - 嵌套括号内的 grep
- javascript - XMLHttpRequest 的 readystatechange 事件在当前脚本结束前触发
- go - golang溢出int64直接操作但没有事先赋值?
- javascript - jQuery单击事件在第一次单击时不起作用
- java - RandomAccessFile 打开文件不起作用
- regex - 仅在 powershell 中的特定单词之后用字符替换空格
- c# - 发送数据包后接收字节时 C# 窗体冻结
- java - JPA EntityManager 不再查找实体,即使恢复到曾经可以工作的代码也是如此