Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a problem in updating the progressbar and textbox at c#.

when i tried this code

C#
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = "1";
            for (int i = 1; i < 11; i++)
            {
                textBox1.Text = (int.Parse(textBox1.Text) + 1).ToString();
                textBox1.Refresh();
                progressBar1.Value += 10;
                progressBar1.Refresh();
                Thread.Sleep(2000);
            }
        }
    }
}


the updating of textbox and progress bar work fine and the text appear on the textbox periodically very well.

i made a program that has server and client to send a file from client to server

i did it and it work very well and i tried it on a file with size 200 Mb

when i tried to make a progressbar and apdate it during the sending and receiving operation it disnot work, it still not progress untill the end of the program go to 100 % suddenly i tried to out the value to a textbox but the text of it not appear untill the end of the program

when i tried to debug i found that the text of the textbox and the progressbar value was updating as i want but why they had no effect untill the end of the program? it is my question

the piece of code that i used the progress bar in is there


C#
//it is a part of the sender of the file
 int i = 1;
 int value = 0;
remainsize = totalsize;//totalsize is the size of the file
while (remainsize > 1024)
{
 data = new byte[1024];
 myfile.Read(data, 0, 1024);//read 1K from the file
 myclient.Client.Send(data);//send the data to the receiver
 data = new byte[2];
 myclient.Client.Receive(data);//wait for OK from the receiver
 if (Encoding.ASCII.GetString(data) != "OK")
 {
      MessageBox.Show("SOMETHING WRONG!!");
       myclient.Close();
       this.Close();
      Application.Exit();
 }
  remainsize -= 1024;
  value+=5;/*increment value here with 5 to test i will update it in my code after solving it*/
  textBox3.Text = value.ToString();//update the textbox
  textBox3.Refresh();//refresh it to appear the text ///do not appear any thing
  progressbar1.value=value;//update the value of the progressbar
  progressbar1.Refresh();
  System.Threading.Thread.Sleep(2000);/* thought the problem in timing and i used this but with no use*/


if you want me to upload all the project tell me and i will do this
Posted
Comments
johannesnestler 24-Jan-12 4:27am    
A "cheap" solution would be a call to Application.DoEvents after updating the values (so the paint events of the controls can do their job). Better would be real threading (not too difficult in your case - consider it)
amertarekt 24-Jan-12 4:33am    
really thank you very very very much

i tried Application.DoEvents and it works well for me
johannesnestler 24-Jan-12 4:45am    
nice to hear, in between Kanasz Robert guided you to the threading solution, you can give it a try.. but nothing is wrong with Application.DoEvents (you can not execute other events while you are in an event (normaly they are queued, but two (AFAIK) are special: Paint and System.Windows.Forms.Timer Tick - they are dropped by the message queue if not executed before a new event arrives (so only one paint and one timer.Tick event is executed in the end) If you compare this to Mouse.Click this behaviour is different.

Hi,

this is because you are using one thread and UI elements are not refreshed regularly. If you want to fix this problem I advice you to create another thread that will process your files and this thread will updates UI elements.

http://blogs.msdn.com/b/csharpfaq/archive/2004/03/17/91685.aspx[^]

http://stackoverflow.com/questions/1136399/how-to-update-textbox-on-gui-from-another-thread-in-c-sharp[^]

http://stackoverflow.com/questions/519233/writing-to-a-textbox-from-another-thread[^]

or in worker thread (thread which process files) use


C#
...
this.Invoke(new MethodInvoker(delegate
{
                textBox1.Text = (int.Parse(textBox1.Text) + 1).ToString();
                textBox1.Refresh();
                progressBar1.Value += 10;
                progressBar1.Refresh();

}));
...


Regards
Robert
 
Share this answer
 
Comments
amertarekt 24-Jan-12 4:53am    
thank you for this great solution i will try it too
The code you posted uses one thread. That means every operation is excuted in sequence right after the previous one finished.
Since you can update GUI elements right away, I suppose the code to be run from main thread (a.k.a "GUI thread"). Blocking the GUI thread results in the GUI ("Graphical User Interface") not updating until there is some idle time for it.

The blocking calls I see are myClient.Client.Receive() and System.Threading.Thread.Sleep().

Don't Sleep() the GUI thread.
Don't use potentially long blocking calls ("Receive()") in the GUI thread.

Let those operations run in a BackgroundWorker[^].
Use its ReportProgress() method and ProgressChanged event to update the GUI.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900