UI not updating
Results 1 to 5 of 5

Thread: UI not updating

  1. #1
    Senior Member
    Join Date
    Apr 2005
    Location
    USA
    Posts
    422

    UI not updating

    I'm having a problem with a program I'm trying to write in C#. I run a long loop of downloading a file and parsing it after you click a button, but the status bar doesn't change until the loop is totally completed. I also had a progress bar, which did update, and go across. If you want to see code, here it is:
    Code:
            void BtnClick(object sender, EventArgs e)
            {
                string url = tb.Text;
                string sea = search.Text;
                string [] lines = sea.Split('\n');
                string searchurl = "";
                int depth = track.Value * 10;
                int queryloop=0;
                int depthloop=0;
                int pro=lines.Length * depth;
                int procount=0;
                double percent=0;
                while(queryloop < lines.Length){
                    depthloop = 0;
                    while(depthloop < depth){
                        //create search query
                        searchurl="http://www.google.com/search?q="+
                            lines[queryloop]
                            +"&hl=en&safe=off&start="+
                            depthloop
                            +"&sa=N";
                        //create download object
                        WebClient Client = new WebClient();
                        //download file
                        Client.DownloadFile(searchurl, " in.txt");
                        depthloop++;
                        procount++;
                        percent=((double)procount/(double)pro)*100;
                        progress.Value=(int)percent;
                        statustext.Text="current url: "+searchurl;
                        
                    }
                    queryloop++;
                }
    basicly what this program is going to do is help with website optimization. I work for a company that wants their page higher on google for many searches, so I'm automating the process of checking the site rank on a specific search by downloading all the files, which is the first 10 pages of results for X amount of search querys, then parsing them. (I haven't gotten to the parsing yet, as you can see in the code) So what happens is as of now, it downloads all the files, as this is happening, the progress bar goes across, but the 'statustext.Text' doesn't show up until the progress bar goes all the way across, which means it is finished anyway. So my question is, how do I make the text display before the loop is finished?

  2. #2
    Senior Member WolfeTone's Avatar
    Join Date
    Jun 2007
    Location
    Ireland
    Posts
    197
    Instead of trying to set the statustext.text inside this function, create another function called set_status_text or something and pass the actual text you wish to set as an argument.

  3. #3
    Senior Member
    Join Date
    Mar 2004
    Posts
    557
    Hi

    There are two different ways to do this.


    1. the ugly way

    Although the status of the 'statustext'-object is dirty,
    ie. there are changes which are not shown, the object is
    not refreshed.

    Thus, you can force a refresh by using

    Code:
      statustext.update()
      // since the control has been invalidated already
    or
    Code:
      statustext.refresh()
      // which essentially is invalidate(true) und update()[1]

    Another hammer is to use the following:

    System.Windows.Forms.Application.DoEvents()

    2. the elegant way

    The above technique works, if the time between the refreshes does not
    exceed 30 seconds. After that the form will become 'white', which reflects
    the unprofessionality of the programmer

    Thus, what you should do is the following: perform the download in a
    new thread, and simply update the form which is in the 'main thread'.
    With such a setup, users even can interact with the form (such as
    pause, resume, stop, ...).

    Luckily, with .net 2.0 this is all trivial: have a look at the backgroundworker[2]-class.


    Cheers

    /edit: Certainly, WolfeTone is right by suggesting a nicer way of system design,
    however, the problem remains. It is better design though, since you simply
    have to add a line
    Code:
      statustext.refresh()
    in your "AddText"-method.


    [1] http://blogs.msdn.com/subhagpo/archi...22/378098.aspx
    [2]
    http://www.agiledeveloper.com/articl...oundWorker.pdf
    http://www.codeguru.com/columns/vb/article.php/c10755/
    http://www.ondotnet.com/pub/a/dotnet...undworker.html
    and others
    Last edited by sec_ware; July 26th, 2007 at 09:24 AM.
    If the only tool you have is a hammer, you tend to see every problem as a nail.
    (Abraham Maslow, Psychologist, 1908-70)

  4. #4
    Senior Member WolfeTone's Avatar
    Join Date
    Jun 2007
    Location
    Ireland
    Posts
    197
    Good post sec_ware!

  5. #5
    Senior Member
    Join Date
    Apr 2005
    Location
    USA
    Posts
    422
    thanks for the help guys. I think i may use the update() anyway, because the program is fairly simple, and the backgroundworker class looks complicated, and I'm not very used to C#..I just use it for the GUI.

Similar Threads

  1. updating definitions question.
    By sb85 in forum AntiVirus Discussions
    Replies: 6
    Last Post: November 7th, 2003, 11:38 PM
  2. Kernel updating problem
    By Zonewalker in forum *nix Security Discussions
    Replies: 8
    Last Post: April 9th, 2003, 02:28 PM
  3. updating redhat linux kernel
    By emPtYKnOw in forum Newbie Security Questions
    Replies: 15
    Last Post: January 2nd, 2003, 05:02 AM
  4. is updating the BIOS really dangerous?
    By manpreet in forum Site Feedback/Questions/Suggestions
    Replies: 35
    Last Post: November 5th, 2002, 08:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •