Problem with Window Form In C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SOB7ANALLAH
    New Member
    • Jul 2008
    • 2

    Problem with Window Form In C#

    I created a form cosists of some controls
    when i press the action control button a some sort of loop occur and take
    some time to finish (about 3 minutes)

    the problem occur when i select another program and comeback to my application,my form showed as white until the process finished

    is it any way to refresh !!!

    help is very appreciated :-)
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Inside your loop, call Application.DoE vents()

    Comment

    • SOB7ANALLAH
      New Member
      • Jul 2008
      • 2

      #3
      Thank you very much plater
      the application work now very well

      Comment

      • IanWright
        New Member
        • Jan 2008
        • 179

        #4
        Originally posted by Plater
        Inside your loop, call Application.DoE vents()
        Plater, is that a preferred method of solving those problem with GUI updates?

        I've not known about that method before and always handled any operations that were going to take a considerable amount of time by firing off another thread, then using delegate to update controls... but that approach looks much easier... I'm just wondering about the benefits/drawbacks of either approach.

        I'd normally do something along the following lines, and enable various buttons etc when a progress bar hits 100%. Just used a label here for simplicity...

        Code:
        private void button_click(object sender, EventArgs e)
        {
           Thread t = new Thread(new ThreadStart(DoStuff));
           t.Start();
        }
        
        private void DoStuff()
        {
           int i = 0;
           While(i < 10000000)
           {
             i++;
             UpdateText(i);
           }
        }
        
        delegate void UpdateDelegate(int i);
        private void UpdateText(int i)
        {
           if(label1.InvokeRequired)
           {
              UpdateDelegate ud = new UpdateDelegate(UpdateText);
              this.Invoke(ud, new object[] { i });
           }
           else
           {
              label1.Text = i.ToString();
           }
        }

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Application.DoE vents() just instructs the program to process any windows messages it has received.
          While using threads and delegates is probably the prefered method, it works in smaller situtations where threads would be overkill

          Comment

          • IanWright
            New Member
            • Jan 2008
            • 179

            #6
            Ok, thanks for that Plater

            Comment

            Working...