SQL Server2008: BeginExecuteNonQuery / EndExecuteNonQuery Problem

I have a stored procedure that writes a backup of a specific database. I call this SP in a C # / Windows Forms application asynchrounously. Below is the code:

IAsyncResult result = command.BeginExecuteNonQuery();
while (!result.IsCompleted)
{
     System.Threading.Thread.Sleep(1000);
     ...
}
command.EndExecuteNonQuery(result));

After a while, the program leaves the loop because IsCompleted = true and calls EndExecuteNonQuery. Now the problem is that Job is still busy and EndExecuteNonQuery is locked ! This will expire the server in a few minutes. It seems that the value of IsCompleted does not match, respectively, what is wrong with IsCompleted ? How can I ensure that my program recognizes the status of real work " ?

+5
source share
1 answer

, (SET NOCOUNT ON). TDS , , ( 1 row updated) , . ? . - ? .

, BeginExecute (...):

// Set form state to 'executing', 
// eg. Button.Enabled = false; label.Text = 'Executing';
command.BeginExecuteNonQuery((asyncstate)=>
{
   try
   {
      command.EndExecuteNotQuery();
   }
   catch(SqlException ex)
   {
     ...
   } 
   // Reset the form state to 'Ready'
   Invoke (()=>
   {
     // eg. Button.Enabled = true; label.Text = 'Ready';
   }
}
+3

All Articles