如何在我的Windows应用程序使用数据库时显示等待消息。
问题是当我显示消息“Please wait ...”时,应用程序在完成数据库操作之前不会响应。
我有一个简单的功能,它在执行时与db交互我想显示一条消息,如“请稍候......”
但我的应用程序不应该挂起,必须响应用户交互。
这是我的功能。
public void getReleaseInfo(Label lblDbVersion)
{
if (gc.logger.IsInfoEnabled)
gc.logger.Info("getReleaseInfo - Method Start");
string l_sConfigPath = gc.APP_CONFIG_FILE_PATH;
var element = XDocument.Load(l_sConfigPath).Descendants("configuration").Descendants("connectionStrings");
if (element != null)
{
foreach (var item in element.Elements("add"))
{
gc.APP_CONNECTIONSTRING = (string)item.Attribute("connectionString");
}
}
try
{
m_oConn = new SqlConnection(gc.APP_CONNECTIONSTRING);
m_oConn.Open();
using (SqlDataAdapter l_oDA = new SqlDataAdapter(gc.SP_GETRELEASEINFO, m_oConn))
{
l_oDA.SelectCommand.CommandType = CommandType.StoredProcedure;
using (DataSet l_oDS = new DataSet())
{
l_oDA.Fill(l_oDS);
if (!l_oDS.HasErrors)
{
if (l_oDS.Tables[0].Rows.Count > 0)
{
lblDbVersion.Text = string.IsNullOrEmpty(l_oDS.Tables[0].Rows[0]["ReleaseInfo"].ToString()) ? "Release information not available" : l_oDS.Tables[0].Rows[0]["ReleaseInfo"].ToString();
}
}
}
}
}
catch (Exception ex)
{
tsslDbError.ForeColor = System.Drawing.Color.Red;
lblDbVersion.Text = "iReg Release Information Not Available";
tsslDbError.Text = "Unable to connect with iReg Database, Please contact iReg Suppot!";
btnBrowse.Enabled = false;
btnBackup.Enabled = false;
}
finally
{
if (m_oConn != null)
{
if (m_oConn.State == ConnectionState.Open)
m_oConn.Close();
m_oConn.Dispose();
}
}
if (gc.logger.IsInfoEnabled)
gc.logger.Info("getReleaseInfo - Method End");
}
从这里我称之为该功能。
try
{
lblDbVersion.Text = string.Empty;
this.tsslDbError.ForeColor = System.Drawing.Color.Black;
tsslDbError.Text = "Please wait... While connecting to iReg Database.";
getReleaseInfo(lblDbVersion);
this.tsslDbError.Text = string.Empty;
}
catch (Exception ex)
{
this.tsslDbError.ForeColor = System.Drawing.Color.Red;
lblDbVersion.Text = "iReg Release Information Not Available";
this.tsslDbError.Text = "Unable to connect with iReg Database, Please contact iReg Suppot!";
btnBrowse.Enabled = false;
btnBackup.Enabled = false;
}
finally
{
}
if (gc.logger.IsInfoEnabled)
gc.logger.Info("btnIregPath_Click - Method End");
}
答案 0 :(得分:3)
您必须在单独的线程中执行数据库调用,以便在数据库调用运行时维护响应式UI。
答案 1 :(得分:1)
我建议你使用Backgroundworker(System.ComponentModel)
你的代码应该是这样的(这是一个样本... ;-)不是一个完美的解决方案,例如 如果发生错误,您将永远不会看到错误填充的标签,但始终为空。我想你可以理解为什么。请注意,您可能会收到一些“不安全的代码警告”,试图更改在不同线程中创建的标签的内容,以避免管理workerprogess更改。
using (BackgroundWorker bgw = new BackgroundWorker())
{
bgw.DoWork += bgw_DoWork;
bgw.RunWorkerCompleted += bgw_RunWorkerCompleted;
this.tsslDbError.ForeColor = System.Drawing.Color.Black;
tsslDbError.Text = "Please wait... While connecting to iReg Database.";
bgw.RunWorkerAsync();
}
当然,你必须添加这些方法:
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.tsslDbError.Text = string.Empty;
if (gc.logger.IsInfoEnabled)
gc.logger.Info("btnIregPath_Click - Method End");
}
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
try
{
lblDbVersion.Text = string.Empty;
getReleaseInfo(lblDbVersion);
}
catch (Exception ex)
{
this.tsslDbError.ForeColor = System.Drawing.Color.Red;
lblDbVersion.Text = "iReg Release Information Not Available";
this.tsslDbError.Text = "Unable to connect with iReg Database, Please contact iReg Suppot!";
btnBrowse.Enabled = false;
btnBackup.Enabled = false;
}
}
答案 2 :(得分:0)
如您所知,由于计算机的速度如果发生此类错误,它将永远不会与您的方法一起显示,因为如果没有像messagebox这样的停止点,则下一个错误将以毫秒为单位覆盖预先存在的错误。显示或逐行写入错误的文本框。
如果错误编号不是很大,我建议您使用日志文本框。