[英]Cancel Main Form closing while some thread runs in back, C# .windows application?
[英]C# : Closing application with non main form
我的應用程序有問題,我想創建一個簡單的登錄頁面,該頁面將通過主頁發送到您(登錄成功),因此我將此代碼放在program.cs上,可以瀏覽通過表單:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Form1 a = new Form1();
a.Show();
Application.Run();
}
}
問題是,如果我在提交之前關閉了我的應用程序(登錄頁面),則該應用程序不會關閉...
謝謝
假設您的登錄頁面是Form1,請將其更改為
Application.Run(a);
通常,您應該這樣啟動應用程序:
Application.Run(a);
或者,您可以在表單的FormClosed事件中執行以下命令:
Application.Exit();
我自己使用此解決方案:
我用:
public static Form1 a = null;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
a = new Form1();
Application.Run(a);
}
並在驗證過程后單擊登錄按鈕時,我使用:
this.Hide();
Form2 form = new Form2();
form.Show();
當我想退出時,我使用:
this.Close();
Program.a.Show();
當我想關閉應用程序時,我使用:
Program.a.Close();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.