如何异步 运行 存储过程并继续 C# 中的其他语句
How to run a stored procedure asynchronously and continue with other statements in C#
以下是我编写的代码,希望我可以异步启动存储过程并获取调用 运行ning 下的代码,以便进行必要的 UI 更改。 但这仅在我从 visual studio 运行 以及我设置断点 时有效。当我删除断点时,它不会 运行 以下行之后的代码:-
IAsyncResult result = command.BeginExecuteNonQuery();
但是,我只是假设它不是 运行 但我不确定,因为我不知道如何正确地知道它是否是 运行ning,仅取决于输出我可以这样说。按钮隐藏或可见性逻辑不起作用。
我想,因为上面的语句是异步的,我可以实现我需要的执行。
我参考了以下内容来实现此代码:-
BeginExecuteNonQuery without EndExecuteNonQuery
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
try
{
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SqlCommandTimeOut"]);
command.CommandText = "RunPackage";
command.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int));
command.Parameters["@UserID"].Value = SessionVariables.UserID;
command.Parameters.Add(new SqlParameter("@JobName", SqlDbType.NVarChar));
command.Parameters["@JobName"].Value = System.Configuration.ConfigurationManager.AppSettings["JobName"].ToString();
conn.Open();
IAsyncResult result = command.BeginExecuteNonQuery();
bool boolresult = GetCurrentRunningJob();
if (boolresult == true)
{
Button1.Enabled = false;
Button1.Visible = false;
Button2.Visible = true;
Button2.Enabled = true;
lblPackageStatus.Text = "The job is already running. Please wait to re-run.";
lblPackageStatus.ForeColor = System.Drawing.Color.Red;
}
else
{
Button2.Enabled = false;
Button2.Visible = false;
Button1.Visible = true;
Button1.Enabled = true;
lblPackageStatus.Text = "";
}
command.EndExecuteNonQuery(result);
}
catch (Exception err)
{
Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex));
}
}
}
您似乎正在检查查询是否是通过调用自定义方法完成的 GetCurrentRunningJob
。
如果是这种情况,则不需要这样做。相反,您可以查看结果上的 IsCompleted 属性 - 如果操作完成,则为 true;否则,false.
IAsyncResult result = command.BeginExecuteNonQuery();
while (!result.IsCompleted)
{
// your code
}
command.EndExecuteNonQuery(result);
以下是我编写的代码,希望我可以异步启动存储过程并获取调用 运行ning 下的代码,以便进行必要的 UI 更改。 但这仅在我从 visual studio 运行 以及我设置断点 时有效。当我删除断点时,它不会 运行 以下行之后的代码:-
IAsyncResult result = command.BeginExecuteNonQuery();
但是,我只是假设它不是 运行 但我不确定,因为我不知道如何正确地知道它是否是 运行ning,仅取决于输出我可以这样说。按钮隐藏或可见性逻辑不起作用。
我想,因为上面的语句是异步的,我可以实现我需要的执行。
我参考了以下内容来实现此代码:- BeginExecuteNonQuery without EndExecuteNonQuery
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
try
{
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandTimeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SqlCommandTimeOut"]);
command.CommandText = "RunPackage";
command.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Int));
command.Parameters["@UserID"].Value = SessionVariables.UserID;
command.Parameters.Add(new SqlParameter("@JobName", SqlDbType.NVarChar));
command.Parameters["@JobName"].Value = System.Configuration.ConfigurationManager.AppSettings["JobName"].ToString();
conn.Open();
IAsyncResult result = command.BeginExecuteNonQuery();
bool boolresult = GetCurrentRunningJob();
if (boolresult == true)
{
Button1.Enabled = false;
Button1.Visible = false;
Button2.Visible = true;
Button2.Enabled = true;
lblPackageStatus.Text = "The job is already running. Please wait to re-run.";
lblPackageStatus.ForeColor = System.Drawing.Color.Red;
}
else
{
Button2.Enabled = false;
Button2.Visible = false;
Button1.Visible = true;
Button1.Enabled = true;
lblPackageStatus.Text = "";
}
command.EndExecuteNonQuery(result);
}
catch (Exception err)
{
Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex));
}
}
}
您似乎正在检查查询是否是通过调用自定义方法完成的 GetCurrentRunningJob
。
如果是这种情况,则不需要这样做。相反,您可以查看结果上的 IsCompleted 属性 - 如果操作完成,则为 true;否则,false.
IAsyncResult result = command.BeginExecuteNonQuery();
while (!result.IsCompleted)
{
// your code
}
command.EndExecuteNonQuery(result);