2016-07-27 78 views
0

我一直在摸索這一塊頭,現在儘管尋找解決方案,我不太瞭解實現(堆棧溢出的幾個答案已經看過)從子線程關閉父窗體

我的程序在打開時會加載一個啓動頁面,在此期間它會檢查數據庫連接。如果有連接,則啓動頁面關閉並加載主窗體,否則會提供錯誤消息,然後完全關閉。

public partial class StartupSplash : Form 
{ 

    ThreadStart th; 
    Thread thread; 

    public StartupSplash() 
    { 
     InitializeComponent(); 

     th = new ThreadStart(DbAvaliable); 
     thread = new Thread(th); 
     thread.Start(); 

    } 

    private void DbAvaliable() 
    { 

     Boolean result = false; 

     using (var connectiontest = new SqlConnection("myConnString")) 
      try 
      { 
       connectiontest.Open(); 
       result = true; 
      } 
      catch (Exception ex) 
      { 
       result = false; 
      } 

     if (result) 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new MainWindow()); 

     } 
     else 
     { 
      MessageBox.Show("Unable to establish database connection. Please check your data connection and try again."); 
     } 
    } 
} 

據我所知,由於交叉線程問題,我不能簡單地致電this.Close()。我讀過一些關於調用方法的內容,但我不太清楚如何實現上述結果。

最初我試圖使用形式加載/顯示的事件,而不是交替的螺紋,但在形成圖像未能加載,直到後在MessageBox已示出的錯誤(而不是顯示,然後運行該連接檢查)

+0

你可以在連接返回true或false的任務中執行db,然後你只需等待它並關閉應用程序或繼續根據結果? – Jacobr365

+2

它是越野車,並會給你造成很大的悲痛。對於啓動畫面,.NET Framework已經有[極好的支持](http://stackoverflow.com/a/393870/17034),而最好使用它。 –

回答

0

與以前的答案一定的幫助張貼Hans Passantherehere),我的解決辦法如下:

static class Program 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main(string[] args) 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     new MyApp().Run(args); 

    } 

class MyApp : WindowsFormsApplicationBase 
{ 
    protected override void OnCreateSplashScreen() 
    { 
     this.SplashScreen = new StartupSplash(); 
    } 

    protected override void OnCreateMainForm() 
    { 
     Boolean result = false; 
     using (var connectiontest = new SqlConnection("myConnectionString")) 
      try 
      { 
       connectiontest.Open(); 
       result = true; 
      } 
      catch (Exception ex) 
      { 
       result = false; 
      } 
     // pause not needed while checking for db connection as that takes its own amount of time to complete. 


     if (result) 
     { 
      System.Threading.Thread.Sleep(3000); //pause moved here to give the splash some time on screen if db connection available 
      this.MainForm = new MainWindow(); 
     } 
     else 
     { 
      MessageBox.Show("Unable to connect to the database"); 

      Environment.Exit(1); // shuts down the program if database connection not avaliable. 

     } 

    } 


} 
0

你可以設置一個事件來觸發Form2與數據庫檢查的結果嗎?如果條件允許,請在Form1上訂閱活動並告訴它關閉。

不知道它是否會工作或沒有,但這樣的:

public Form2 : Form 
{ 
    public delegate void DbCheckHandler(object sender, DbEventArgs e); 
    public event DbCheckHandler DbCheckComplete; 

    //check the db 
    DbCheckComplete(this, new DbEventArgs { ShouldClose = true; }); 

} 

public Form1 : Form 
{ 
    Form2 form2 = new Form2(); 
    form2.DbCheckComplete += new DbCheckHandler(CheckDbResult); 
    form2.Show(); 

    private void CheckDbResult(object sender, DbEventArgs e) 
    { 
     if(e.ShouldClose) 
     { 
      this.Close(); 
     } 

    } 
} 
+0

不是我真正想要的東西,但是非常感謝! – Takarii