2015-05-04 60 views
1

我有一個WinForm調用SpalshScreen.cs帶有一個簡單的標籤,Text屬性設置爲「Data Loading ...」。標籤以表格爲中心。我也有一個名爲DoClose()的公開方法定義。爲什麼我的SplashScreen不顯示標籤(文本)

MainForm.Form_Load方法包括:

Hide(); 
SplashScreen form = new SplashScreen(); 
form.Show(); 

// Simulate Doing Database Table Load(s) 
Thread.Sleep(5000); 

form.DoClose(); 
Show(); 

然而,當我跑,我飛​​濺確實出現,但是,這裏的標識文字,假設是隻顯示淺色箱。

如果我將form.Show();更改爲form.ShowDialog();,文本顯示正確,但主循環會暫停,直到關閉Splash窗口。

+4

將數據庫加載到非UI線程中,而不是在UI線程中。 – Servy

+0

不,他們都加載在UI線程中。 – Randy

+2

這不是一個問題,而是一個建議。 – walther

回答

1

經過一堆試驗和錯誤...訣竅是不要屏蔽UI線程,因爲@Servy說。

需要改變到Form_Load方法:

Hide(); 
Splash.Show(); 

// Everything after this line must be Non UI Thread Blocking 
Task task = new Task(LoadDataAsync); 
task.Start(); 
task.Wait(); 

Splash.DoClose(); 
Show(); 

我開了一LoadDataAsync方法打理一切的:

private async void LoadDataAsync() 
    { 
     await Context.Employees.LoadAsync(); 
     await Context.Customers.LoadAsync(); 

     // The Invoke/Action Wrapper Keeps Modification of UI Elements from 
     // complaining about what thread they are on. 
     if (EmployeeDataGridView.InvokeRequired) 
     { 
      Action act =() => EmployeeBindingSource.DataSource = Context.Employees.Local.ToBindingList(); 
      EmployeeDataGridView.Invoke(act); 
     } 

     if (CustomerComboBox.InvokeRequired) 
     { 
      Action act =() => 
      { 
       CustomerBindingSource.DataSource = GetCustomerList(); 
       CustomerComboBox.SelectedIndex = -1; 
      }; 
      CustomerComboBox.Invoke(act); 
     } 
    } 

我還設置任何私有字段和私有方法的我正在使用靜態。

0

在splashscreen窗體中使用計時器而不是thread.sleep(例如5秒後關閉啓動屏幕),並設置它的關閉事件。

var form = new SplashScreen(); 
form.Closed += (s,e)=>{ 
    Show(); 
} 
form.Show(); 
相關問題