2015-05-31 49 views
0

奇怪的錯誤。我有以下代碼。無法更改標籤文字

private void connectButton_Click(object sender, EventArgs e) 
{ 
    statusLabel.Text = "Connecting..."; 
    statusLabel.ForeColor = Color.Green; 
    serverNameBox.Enabled = false; 
    databaseNameBox.Enabled = false; 
    connectButton.Enabled = false; 
    conn = new SqlConnection("server=" + serverNameBox.Text + ";Trusted_Connection=yes;database=" + databaseNameBox.Text + ";connection timeout=3"); 
    try 
    { 
     conn.Open(); 
    } 
    catch (Exception ex) 
    { 
     statusLabel.Text = "Connection Failed"; 
     statusLabel.ForeColor = Color.DarkRed; 
     MessageBox.Show("Connection Failed. Error message below:\n" + ex.Message); 
     serverNameBox.Enabled = true; 
     databaseNameBox.Enabled = true; 
     connectButton.Enabled = true; 
     return; 
    } 
    statusLabel.Text = "Connected Successfully"; 
    statusLabel.ForeColor = Color.DarkGreen; 
    serverNameBox.Enabled = true; 
    connectButton.Enabled = true; 
    conn.Close(); 
    UpdateTraders(); 
    UpdateTransactions(); 
} 

「連接成功」和「連接失敗」都工作得不錯。但是,statusLabel永遠不會更改爲「正在連接」。 statusLabel.Text的默認值是「」(無)。

這是怎麼回事?

回答

0

由於您在UI線程中更改文本並對其進行了阻止,因此會發生此問題。

您可以在更改標籤文本後使用Application.DoEvents()解決方法,但最好的方法是使用多線程。例如,您可以使用BackgroundWorker類來實現此目的。

+0

也可以使用'DbConnection.OpenAsync()'方法(如果使用Framework 4.5)。 –

-1

實際上「連接...」文本沒有在UI線程中顯示,但它確實工作正常。

因爲在這種方法中,當執行第一行statusLabel.Text = "Connecting...";後,狀態標籤文本將會暫時改變,但並未顯示在表單UI中,然後代碼繼續執行狀態標籤的文本將被替換爲statusLabel.Text = "Connection Failed"; conn失敗或statusLabel.Text = "Connected Successfully"; conn成功時。

所以在UI線程中,我們只看到文本更改爲「連接失敗」或「連接成功」。

調試下,插入斷點statusLabel.Text = "Connecting...";線,當按F6步驟結束後,你可以看到通過調試當地人窗口改變了statuslabel文本。

希望有幫助。謝謝。

+0

實際上'Label'只會在執行'Button.Click'事件處理程序後纔會更新到GUI。放置'Thread.Sleep(5000)'之間,你會看到該標籤不更新 – Fabio

+0

快速狀態更改不是原因。當你設置一個斷點時,你會在dubugger中看到'statusLabel'.Text'暫時變成了「正在連接...」,但是你永遠不會在UI上看到這個值。正如用戶@Jaex解釋的那樣,用戶界面只會在'connectButton_Click()'後更新。訣竅是讓用戶界面有機會做一個Update DURING'connectButton_Click()'。一般選項是例如:'await'和'Application.DoEvents()'。在這種特殊情況下:'DbConnection.OpenAsync()'。 – JimiLoe

0

如果您使用.Net 4.5或更高版本,則可以使用async來保持UI流動。

更改您的代碼如下所示(請注意使用的asyncawait關鍵字):

private async void connectButton_Click(object sender, EventArgs e) 
{ 
    statusLabel.Text = "Connecting..."; 
    statusLabel.ForeColor = Color.Green; 
    serverNameBox.Enabled = false; 
    databaseNameBox.Enabled = false; 
    connectButton.Enabled = false; 
    conn = new SqlConnection("server=" + serverNameBox.Text + ";Trusted_Connection=yes;database=" + databaseNameBox.Text + ";connection timeout=3"); 
    try 
    { 
     await conn.OpenAsync(); 
    } 
    catch (Exception ex) 
    { 
     statusLabel.Text = "Connection Failed"; 
     statusLabel.ForeColor = Color.DarkRed; 
     MessageBox.Show("Connection Failed. Error message below:\n" + ex.Message); 
     serverNameBox.Enabled = true; 
     databaseNameBox.Enabled = true; 
     connectButton.Enabled = true; 
     return; 
    } 
    statusLabel.Text = "Connected Successfully"; 
    statusLabel.ForeColor = Color.DarkGreen; 
    serverNameBox.Enabled = true; 
    connectButton.Enabled = true; 
    conn.Close(); 
    UpdateTraders();  // This might need the async treatment too 
    UpdateTransactions(); // This might need the async treatment too 
} 

然而,這可能是不夠的,這取決於UpdateTraders()UpdateTransactions()做。他們可能也需要做異步,並且一些慢速調用轉換爲await ...(假設他們支持它)。如果從Windows應用程序中使用

System.Thread.Sleep(5000); 

,把進度條對這項工作:

0

您可以從這段代碼中的睡眠系統,持續5秒使用,你可以把圖像加載了這項工作。