【发布时间】:2014-01-30 06:24:35
【问题描述】:
如何在我的 Windows 应用程序使用数据库时显示等待消息。
问题是当我显示消息“请稍候...”时,应用程序在完成数据库操作之前没有响应。
我有一个简单的函数,它在执行时与 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");
}
【问题讨论】:
-
如果您使用的是 .net 4.5,那么您可以使用 async 和 await 来实现这一点。