Problem with faking a progress bar / threading.

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

    Problem with faking a progress bar / threading.

    For an application I'm creating I want to create a 'fake' progress
    bar.
    By fake I mean a progress bar that looks like it's doing something but
    actually isn't.

    I know philosophically this isn't sound. But my little app is a 'fake'
    app and is designed to look like another - hence this seeming crazy
    situation of needing to fake a progess bar.

    PROBLEM.
    I can not figure out how to delay the execution of my code to pause
    between updates of the progress bar value, I had thought i could do
    something like this.

    progressbar1.va lue = 10;
    wait for 20 seconds....
    progressbar1.va lue=50;
    wait for 10 seconds....
    progressbar1.va lue=90;

    etc... The problem I have is that the only method i've managed to find
    of pausing code execution is the thread.sleep method. However this
    causes the form not to be displayed, from what i've gathered this is
    because the part of the code that actually paints the form is
    disrupted by the call to make the thread sleep.

    My Attempted Solution.

    I tried to create a new thread and put the code that deals with the
    progress bar in that thread. This created an error about cross thread
    referencing. I then disabled cross thread referencing using
    Control.CheckFo rIllegalCrossTh readCalls = false; but it's still not
    working.

    Any ideas how to fix this.
    Here is what I have so far.
    =============== ======
    using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows. Forms;

    namespace Stealth
    {
    public partial class Form3 : Form
    {

    public Form3()
    {
    InitializeCompo nent();
    Control.CheckFo rIllegalCrossTh readCalls = false;

    }
    private void myStartingMetho d()
    {
    //change value to 20.
    progressBar1.Va lue = 20;
    //pause
    System.Threadin g.Thread.Sleep( 5000);
    //change value to 90.
    progressBar1.Va lue = 90;


    }
    private void Form3_Load(obje ct sender, EventArgs e)
    {

    InitializeCompo nent();

    System.Threadin g.Thread myThread;
    myThread = new System.Threadin g.Thread(new
    System.Threadin g.ThreadStart(m yStartingMethod ));


    myThread.Start( );

    }


    }
    }
    =============== ==
    TIA.

  • Matt Lacey

    #2
    Re: Problem with faking a progress bar / threading.

    On 20 Sep, 13:02, CCLeasing <g...@ccleasing .co.ukwrote:
    For an application I'm creating I want to create a 'fake' progress
    bar.
    By fake I mean a progress bar that looks like it's doing something but
    actually isn't.
    >
    I know philosophically this isn't sound. But my little app is a 'fake'
    app and is designed to look like another - hence this seeming crazy
    situation of needing to fake a progess bar.
    >
    PROBLEM.
    I can not figure out how to delay the execution of my code to pause
    between updates of the progress bar value, I had thought i could do
    something like this.
    >
    progressbar1.va lue = 10;
    wait for 20 seconds....
    progressbar1.va lue=50;
    wait for 10 seconds....
    progressbar1.va lue=90;
    >
    etc... The problem I have is that the only method i've managed to find
    of pausing code execution is the thread.sleep method. However this
    causes the form not to be displayed, from what i've gathered this is
    because the part of the code that actually paints the form is
    disrupted by the call to make the thread sleep.
    >
    My Attempted Solution.
    >
    I tried to create a new thread and put the code that deals with the
    progress bar in that thread. This created an error about cross thread
    referencing. I then disabled cross thread referencing using
    Control.CheckFo rIllegalCrossTh readCalls = false; but it's still not
    working.
    >
    Any ideas how to fix this.
    Here is what I have so far.
    =============== ======
    using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows. Forms;
    >
    namespace Stealth
    {
    public partial class Form3 : Form
    {
    >
    public Form3()
    {
    InitializeCompo nent();
    Control.CheckFo rIllegalCrossTh readCalls = false;
    >
    }
    private void myStartingMetho d()
    {
    //change value to 20.
    progressBar1.Va lue = 20;
    //pause
    System.Threadin g.Thread.Sleep( 5000);
    //change value to 90.
    progressBar1.Va lue = 90;
    >
    }
    private void Form3_Load(obje ct sender, EventArgs e)
    {
    >
    InitializeCompo nent();
    >
    System.Threadin g.Thread myThread;
    myThread = new System.Threadin g.Thread(new
    System.Threadin g.ThreadStart(m yStartingMethod ));
    >
    myThread.Start( );
    >
    }
    >
    }}
    >
    =============== ==
    TIA.
    It doesn't solve your threading problem, but using a timer to
    increment the progress bar will have the same desired effect:
    add a progressbar, button & timer to your form and try the following
    code to see how it might work:

    private void timer1_Tick(obj ect sender, System.EventArg s e)
    {
    if (progressBar1.V alue < 100)
    {
    progressBar1.Va lue = progressBar1.Va lue + 5;
    }
    }

    private void button1_Click(o bject sender, System.EventArg s e)
    {
    progressBar1.Va lue = 0;
    }

    private void Form1_Load(obje ct sender, System.EventArg s e)
    {
    timer1.Enabled = true;
    }

    Adjust the timer interval and the amount the progress bar increments
    as needed.

    Comment

    • william.w.oneill@gmail.com

      #3
      Re: Problem with faking a progress bar / threading.

      In your myStartingMetho d method, look at using progressBar1.In voke()
      or BeginInvoke(). These methods provide the functionality you need.

      Comment

      • Chris Shepherd

        #4
        Re: Problem with faking a progress bar / threading.

        CCLeasing wrote:
        For an application I'm creating I want to create a 'fake' progress
        bar.
        By fake I mean a progress bar that looks like it's doing something but
        actually isn't.
        >
        I know philosophically this isn't sound. But my little app is a 'fake'
        app and is designed to look like another - hence this seeming crazy
        situation of needing to fake a progess bar.
        Use a BackgroundWorke r and set it to report its progress. This is what
        it is designed for.

        Chris.

        Comment

        • Arnshea

          #5
          Re: Problem with faking a progress bar / threading.

          On Sep 20, 8:02 am, CCLeasing <g...@ccleasing .co.ukwrote:
          For an application I'm creating I want to create a 'fake' progress
          bar.
          By fake I mean a progress bar that looks like it's doing something but
          actually isn't.
          >
          I know philosophically this isn't sound. But my little app is a 'fake'
          app and is designed to look like another - hence this seeming crazy
          situation of needing to fake a progess bar.
          >
          PROBLEM.
          I can not figure out how to delay the execution of my code to pause
          between updates of the progress bar value, I had thought i could do
          something like this.
          >
          progressbar1.va lue = 10;
          wait for 20 seconds....
          progressbar1.va lue=50;
          wait for 10 seconds....
          progressbar1.va lue=90;
          >
          etc... The problem I have is that the only method i've managed to find
          of pausing code execution is the thread.sleep method. However this
          causes the form not to be displayed, from what i've gathered this is
          because the part of the code that actually paints the form is
          disrupted by the call to make the thread sleep.
          >
          My Attempted Solution.
          >
          I tried to create a new thread and put the code that deals with the
          progress bar in that thread. This created an error about cross thread
          referencing. I then disabled cross thread referencing using
          Control.CheckFo rIllegalCrossTh readCalls = false; but it's still not
          working.
          >
          Any ideas how to fix this.
          Here is what I have so far.
          =============== ======
          using System;
          using System.Collecti ons.Generic;
          using System.Componen tModel;
          using System.Data;
          using System.Drawing;
          using System.Text;
          using System.Windows. Forms;
          >
          namespace Stealth
          {
          public partial class Form3 : Form
          {
          >
          public Form3()
          {
          InitializeCompo nent();
          Control.CheckFo rIllegalCrossTh readCalls = false;
          >
          }
          private void myStartingMetho d()
          {
          //change value to 20.
          progressBar1.Va lue = 20;
          //pause
          System.Threadin g.Thread.Sleep( 5000);
          //change value to 90.
          progressBar1.Va lue = 90;
          >
          }
          private void Form3_Load(obje ct sender, EventArgs e)
          {
          >
          InitializeCompo nent();
          >
          System.Threadin g.Thread myThread;
          myThread = new System.Threadin g.Thread(new
          System.Threadin g.ThreadStart(m yStartingMethod ));
          >
          myThread.Start( );
          >
          }
          >
          }}
          >
          =============== ==
          TIA.
          Calls that update a UI object have to be marshaled onto the UI
          thread. I find the following pattern helpful:

          void MethodThatUpdat esUI()
          {
          if (InvokeRequired )
          Invoke(new MethodInvoker(M ethodThatUpdate sUI));
          else
          {
          // update whatever ui controls
          }
          }

          The only downside is the proliferation of one-off delegates if you
          need to pass data into/out of the method.

          Comment

          • Peter Duniho

            #6
            Re: Problem with faking a progress bar / threading.

            Arnshea wrote:
            Calls that update a UI object have to be marshaled onto the UI
            thread. I find the following pattern helpful:
            >
            void MethodThatUpdat esUI()
            {
            if (InvokeRequired )
            Invoke(new MethodInvoker(M ethodThatUpdate sUI));
            else
            {
            // update whatever ui controls
            }
            }
            >
            The only downside is the proliferation of one-off delegates if you
            need to pass data into/out of the method.
            That latter issue is not a problem at all with the use of anonymous methods.

            However, I don't like the general pattern myself, because of the
            proliferation of methods that do two completely different things
            depending on what thread they are on.

            I prefer to either have a single method always used regardless of thread
            and which always calls Invoke(), or to simply have the thread be aware
            of whether it needs to call Invoke() itself.

            That said, IMHO Matt's answer was the best in this situation, since no
            actual work is being done in a background thread. All that's really
            necessary is to update the progress bar periodically, and a Timer is a
            fine way to do that without introducing any of the cross-thread issues
            while still preserving a responsive UI.

            Pete

            Comment

            Working...