Richtextbox dosnt change content till after sql exection is complete

Gerald Oakham 101 Reputation points
2022-06-20T09:35:35.397+00:00

Hi,
I am trying to construct a c# Windows application that backs up a predefined database.
the backup process works fine, but I am trying to change a Richatext box ( and a label) to show what is happening..

The RTB clears when the backup button is pressed, but, before the SQL process is called, I also add some text tot he box 9 and change a label from "Idle" to "Processing ".
the RTB doesn't change until after the SQL process is run (I am assuming the same for the Lable, but part of the end process is to change its text back to "idle".

It doesn't seem to matter where I put code to change the RTB and Label, it just doesn't happen.

My plan was to show the "STATS" results in the RTB, but I cannot do this, and from searching the internet, I should use MSO instead ( I have no idea really how to do this, so at the moment I am going for Text change @ being of process, and additional text once completed.

Not, I think my problem is because the BeginEcecuteNonQuery is assigned to an IAsyncResult ( which might also account for why the application window is "Not responding" when I try and move it around.

My question a) Am I right about the IASyncResult (and if not, what would it be) affecting the RBT not changing at the beginning of the process? , and b) how do I fix it.

Also, if anyone can show me how I would get a "show stats" like results during the backup process, that would be great :-)

my code at the moment is :

richTextBox1.Clear();  
   //TEXT that wont change  
                richTextBox1.AppendText(DateTime.Now + " - Backing up " + _dataINICat + " to " + textBox2.Text + "." + Environment.NewLine);  
      
                label3.Text = "Processing...";  
      //TEXT that wont change  
try  
{  
using (sqlconnection newConnection - new SqlConnection(connectionString))  
{  
try  
{  
sqlcommand command = new sqlCommand(backupquery, newconnection);  
newconnection.Opsn().  
IasyncResults results = command.BeginExecuteNonQuery();  
While (!results.ISComplete)  
{  
richtextboz1.Appendtext("Waiting " + count ++);  
}   
richtextbox1.appendtext ("command completed");  
}  
  
// some CATCH statements   
newconnection.close();  
lable3.text = "Idle";  

Thanks for taking the time to read this. sorry about the code, but I kept getting "WAF v2 has determined your request exceeded the normal web request and has blocked your request." when trying to post the completed version.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,713 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,247 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 112.1K Reputation points
    2022-06-20T09:57:31.577+00:00

    Since you have a blocking form, try executing richTextBox1.Update after AppendTest.

    It is also possible to write a non-blocking code using threads, for example.

    Or maybe remove the while loop, exit from the function, and use a periodic timer to check the status of IAsyncResult.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Gerald Oakham 101 Reputation points
    2022-06-24T09:29:07.82+00:00

    this is great. thank you so much for your help

    0 comments No comments