2015-05-31 52 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秒使用,你可以把图像加载了这项工作。