Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

C# Form Freezes 1

Status
Not open for further replies.

kalkalai

Technical User
Feb 24, 2006
33
KZ
Hi,

I have a C# program... which reads SQL scripts and connects to Oracle. By the way I have put the RichTextBox while it is processing I am appending text there so that I see the start time before the sql script is requested, then some middle texts... But the problem is my window freezes while it does the request from oracle, and only after that it shows all my buttons, richtextbox, etc... and all the text is only displayed after the execution of the sql script...

How can i make it to show everything while it is on that line of code in my program? thanx in advance.
 
This is where Multithreading comes into play.

You need a worker thread to talk to the Oracle DB which Invokes updates onto your main thread (the one your form is on)

A simple example is a progress bar. Google for a progress bar in C# and you should find your answer.

 
Hi JurkMonkey,

for example i have a thread which does smth {....}

but while it does smth inside it i want richtextbox add some texts...

inside my thread i am doing this

{
...
...
Thread.Sleep(1);
richtextbox.appendtext(.... names...);
....
....

}




it does not run this, why? thanks in advance!
 
Hi Kalkalai,

Looks like you are trying to update the textbox from a thread other than the UI thread. This is not guaranteed to work and can problems.

You need to do an invoke onto the UI thread to do this. Example below:

Code:
{
...
...
Thread.Sleep(1);
UpdateTextBox(.... names[i]...);
....
....

}

private void UpdateTextBox(.... names ...){
  if (this.InvokeRequired){
    // called from a different thread - invoke onto the UI thread
    Delegate del = (MethodInvoker)delegate(){ UpdateTextBox(names);
  } else{
     richTextBox.AppendText(names);
  }

}

Hope this helps... You may also want to sleep a little longer.

"Just beacuse you're paranoid, don't mean they're not after you
 
Hi MadJock & JurkMonkey,

Thanks a lot both of you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top