Puzzled about Windows Form controls behavior

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Blip

    Puzzled about Windows Form controls behavior

    Here is the program. The appropriate controls exist in default
    configuration on form1. I don't understand why the progressbar
    updates, yet the textbox, up/down control, toolStripStatus Label &
    label do not. What am I missing here?

    Thanks, Tom

    using System;
    using System.Threadin g;
    using System.Windows. Forms;

    namespace ProgressBarTest 1
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeCompo nent();
    }

    protected override void OnLoad(System.E ventArgs e)
    {
    toolStripStatus Label1.Text = "Idling!!!" ;
    }

    private void UpdateProgressB ar()
    {
    progressBar1.Vi sible = true;
    progressBar1.Ma ximum = 200;
    progressBar1.Mi nimum = 0;
    progressBar1.St ep = 10;

    for (int x = 1; x <= 200; x++)
    {
    if (x >= 200)
    x = 1;

    label1.Text = x.ToString();
    toolStripStatus Label1.Text = "Incrementing!! !";
    numericUpDown1. Text = x.ToString();
    textBox1.Text = x.ToString();

    progressBar1.Va lue = x;
    progressBar1.Pe rformStep();

    Thread.Sleep(10 );
    }
    }

    private void btnStart_Click( object sender, EventArgs e)
    {
    toolStripStatus Label1.Text = "Starting!! !";
    UpdateProgressB ar();
    }

    private void btnClose_Click( object sender, EventArgs e)
    {
    progressBar1.Di spose();
    Application.Exi t();
    }
    }
    }
  • Jon Skeet [C# MVP]

    #2
    Re: Puzzled about Windows Form controls behavior

    On Sep 23, 3:23 pm, Blip <b...@krumpli.c omwrote:
    Here is the program. The appropriate controls exist in default
    configuration on form1. I don't understand why the progressbar
    updates, yet the textbox, up/down control, toolStripStatus Label &
    label do not. What am I missing here?
    You're never letting the UI thread take control again and react to all
    the changes. Some controls will be okay with that and repaint
    immediately - some won't. The key is just to avoid blocking the UI
    thread in this way in the first place.

    Jon

    Comment

    • Chris Dunaway

      #3
      Re: Puzzled about Windows Form controls behavior

      On Sep 23, 9:23 am, Blip <b...@krumpli.c omwrote:
      Here is the program. The appropriate controls exist in default
      configuration on form1. I don't understand why the progressbar
      updates, yet the textbox, up/down control, toolStripStatus Label &
      label do not. What am I missing here?
      >
      Thanks, Tom
      >
      using System;
      using System.Threadin g;
      using System.Windows. Forms;
      >
      namespace ProgressBarTest 1
      {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeCompo nent();
      }
      >
      protected override void OnLoad(System.E ventArgs e)
      {
      toolStripStatus Label1.Text = "Idling!!!" ;
      }
      >
      private void UpdateProgressB ar()
      {
      progressBar1.Vi sible = true;
      progressBar1.Ma ximum = 200;
      progressBar1.Mi nimum = 0;
      progressBar1.St ep = 10;
      >
      for (int x = 1; x <= 200; x++)
      {
      if (x >= 200)
      x = 1;
      >
      label1.Text = x.ToString();
      toolStripStatus Label1.Text = "Incrementing!! !";
      numericUpDown1. Text = x.ToString();
      textBox1.Text = x.ToString();
      >
      progressBar1.Va lue = x;
      progressBar1.Pe rformStep();
      >
      Thread.Sleep(10 );
      }
      }
      >
      private void btnStart_Click( object sender, EventArgs e)
      {
      toolStripStatus Label1.Text = "Starting!! !";
      UpdateProgressB ar();
      }
      >
      private void btnClose_Click( object sender, EventArgs e)
      {
      progressBar1.Di spose();
      Application.Exi t();
      }
      }
      >
      }
      The PerformStep method increments the Value property and then
      updates. That's probably the difference. In normal code you would
      not need both lines. Try commenting it out and see what happens.

      Chris

      Comment

      • Blip

        #4
        Re: Puzzled about Windows Form controls behavior

        On Tue, 23 Sep 2008 07:46:50 -0700 (PDT), "Jon Skeet [C# MVP]"
        <skeet@pobox.co mwrote:
        >On Sep 23, 3:23 pm, Blip <b...@krumpli.c omwrote:
        >Here is the program. The appropriate controls exist in default
        >configuratio n on form1. I don't understand why the progressbar
        >updates, yet the textbox, up/down control, toolStripStatus Label &
        >label do not. What am I missing here?
        >
        >You're never letting the UI thread take control again and react to all
        >the changes. Some controls will be okay with that and repaint
        >immediately - some won't. The key is just to avoid blocking the UI
        >thread in this way in the first place.
        >
        >Jon
        OK --

        I tried different combinations of anaonymous methods on those controls
        that would allow it - an example is below. What I think I am doing is
        moving progressbar, label, & textbox to their own threads & that's
        what I think I am seeing in the thread debug window. Only the
        progressbar control repaints...

        Do I need to move UpdateProgressB ar() onto its own thread? I'm
        confiused about this stuff.

        private void UpdateProgressB ar()
        {
        progressBar1.Vi sible = true;
        progressBar1.Ma ximum = 200; // max =
        wellsounder.tot allogfiles.item s.number
        progressBar1.Mi nimum = 0;
        progressBar1.St ep = 10;

        for (int x = 1; x <= 200; x++)
        {
        if (x >= 200)
        x = 1;

        toolStripStatus Label1.Text = "Incrementing!! !";
        label1.Invoke(n ew MethodInvoker(d elegate { label1.Text
        = x.ToString(); }));
        numericUpDown1. Invoke(new MethodInvoker(d elegate {
        numericUpDown1. Text = x.ToString(); }));
        textBox1.Invoke (new MethodInvoker(d elegate {
        textBox1.Text = x.ToString(); }));
        //textBox1.Text = x.ToString();
        progressBar1.In voke(new MethodInvoker(d elegate {
        progressBar1.Va lue = x; }));

        Thread.Sleep(10 0);
        }
        }

        Thanks

        Comment

        • Blip

          #5
          Re: Puzzled about Windows Form controls behavior

          On Tue, 23 Sep 2008 07:59:27 -0700 (PDT), Chris Dunaway
          <dunawayc@gmail .comwrote:
          >On Sep 23, 9:23 am, Blip <b...@krumpli.c omwrote:
          >Here is the program. The appropriate controls exist in default
          >configuratio n on form1. I don't understand why the progressbar
          >updates, yet the textbox, up/down control, toolStripStatus Label &
          >label do not. What am I missing here?
          >>
          >Thanks, Tom
          >>
          >using System;
          >using System.Threadin g;
          >using System.Windows. Forms;
          >>
          >namespace ProgressBarTest 1
          >{
          > public partial class Form1 : Form
          > {
          > public Form1()
          > {
          > InitializeCompo nent();
          > }
          >>
          > protected override void OnLoad(System.E ventArgs e)
          > {
          > toolStripStatus Label1.Text = "Idling!!!" ;
          > }
          >>
          > private void UpdateProgressB ar()
          > {
          > progressBar1.Vi sible = true;
          > progressBar1.Ma ximum = 200;
          > progressBar1.Mi nimum = 0;
          > progressBar1.St ep = 10;
          >>
          > for (int x = 1; x <= 200; x++)
          > {
          > if (x >= 200)
          > x = 1;
          >>
          > label1.Text = x.ToString();
          > toolStripStatus Label1.Text = "Incrementing!! !";
          > numericUpDown1. Text = x.ToString();
          > textBox1.Text = x.ToString();
          >>
          > progressBar1.Va lue = x;
          > progressBar1.Pe rformStep();
          >>
          > Thread.Sleep(10 );
          > }
          > }
          >>
          > private void btnStart_Click( object sender, EventArgs e)
          > {
          > toolStripStatus Label1.Text = "Starting!! !";
          > UpdateProgressB ar();
          > }
          >>
          > private void btnClose_Click( object sender, EventArgs e)
          > {
          > progressBar1.Di spose();
          > Application.Exi t();
          > }
          > }
          >>
          >}
          >
          >The PerformStep method increments the Value property and then
          >updates. That's probably the difference. In normal code you would
          >not need both lines. Try commenting it out and see what happens.
          >
          >Chris
          You are correct, Thanks

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Puzzled about Windows Form controls behavior

            Blip <blip@krumpli.c omwrote:
            I tried different combinations of anaonymous methods on those controls
            that would allow it - an example is below. What I think I am doing is
            moving progressbar, label, & textbox to their own threads & that's
            what I think I am seeing in the thread debug window. Only the
            progressbar control repaints...
            No, you're still doing everything from the same thread. Calling Invoke
            forces the delegate to be executed on the UI thread, but as you're on
            the UI thread anyway it makes very little difference.
            Do I need to move UpdateProgressB ar() onto its own thread? I'm
            confiused about this stuff.
            I don't have time to give a full example now, but hopefully
            http://pobox.com/~skeet/csharp/threads/winforms.html will help. Also
            look at BackgroundWorke r (which didn't exist when I wrote that page).

            --
            Jon Skeet - <skeet@pobox.co m>
            Web site: http://www.pobox.com/~skeet
            Blog: http://www.msmvps.com/jon.skeet
            C# in Depth: http://csharpindepth.com

            Comment

            • Jon Skeet [C# MVP]

              #7
              Re: Puzzled about Windows Form controls behavior

              Jon Skeet [C# MVP] <skeet@pobox.co mwrote:
              I don't have time to give a full example now, but hopefully
              http://pobox.com/~skeet/csharp/threads/winforms.html will help. Also
              look at BackgroundWorke r (which didn't exist when I wrote that page).
              Sorry, that link should be
              Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.


              --
              Jon Skeet - <skeet@pobox.co m>
              Web site: http://www.pobox.com/~skeet
              Blog: http://www.msmvps.com/jon.skeet
              C# in Depth: http://csharpindepth.com

              Comment

              Working...