(应用程序冻结示例-山是我的桌面背景)
如果让它保持静止约5〜10分钟,它就会重新运行,并且在之后(即使在锁定计算机/屏幕保护弹出后)不会再挂起,直到重新启动应用程序。这很难调试,因为它只发生在从Visual Studio启动程序时,而不是手动打开.exe时。 只有在显示启动画面画面时才会发生这种情况 - 如果我删除显示启动画面画面的代码,它就不会发生。但我们需要启动画面画面。 我已经尝试了此页面上的每个建议,唯一不会出现问题的是使用
Microsoft.VisualBasic.WindowsFormsApplicationBase
,但这会导致所有种类其他问题。关于这个问题的信息在互联网上似乎很少 - 有人遇到过类似的问题吗?
以下是相关代码:
//Multiple programs use this login form, all have the same issue
public partial class LoginForm<TMainForm>
where TMainForm : Form, new()
{
private readonly Action _showLoadingForm;
public LoginForm(Action showLoadingForm)
{
...
_showLoadingForm = showLoadingForm;
}
private void btnLogin_Click(object sender, EventArgs e)
{
...
this.Hide();
ShowLoadingForm(); //Problem goes away when commenting-out this line
new TMainForm().ShowDialog();
this.Close();
}
private void ShowLoadingForm()
{
Thread loadingFormThread = new Thread(o => _showLoadingForm());
loadingFormThread.IsBackground = true;
loadingFormThread.SetApartmentState(ApartmentState.STA);
loadingFormThread.Start();
}
}
以下是一个程序中使用的_showLoadingForm
动作的示例:
public static bool _showSplash = true;
public static void ShowSplashScreen()
{
//Ick, DoEvents! But we were having problems with CloseSplashScreen being called
//before ShowSplashScreen - this hack was found at
//https://dev59.com/NHVD5IYBdhLWcg3wNY5Z/48946#48946
using(SplashForm splashForm = new SplashForm())
{
splashForm.Show();
while(_showSplash)
Application.DoEvents();
splashForm.Close();
}
}
//Called in MainForm_Load()
public static void CloseSplashScreen()
{
_showSplash = false;
}
MainForm.ShowDialog
内部吗?还是在调用堆栈更上层有更具体的东西。如果它确实卡在ShowDialog
内部,那么这意味着主 UI 线程上不再传递消息。这非常奇怪,可能表明 .NET 中存在一个晦涩的 bug,只有在显示闪屏时才会显现出来。这是一个相当奇怪的问题。 - Brian GideonApplication.DoEvents
的 while 循环应该是不停地旋转。它消耗了很多 CPU 时间吗? - Brian Gideonvolatile
的,顺便说一下。 - Remus Rusanu