Thread.Sleep

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

    Thread.Sleep

    If you want to keep reading the value of a property (10 seconds) that
    keeps changing,
    how would you do it. I tried this, but it did not work


    Thread th = new Thread(new ThreadStart(Rea dMe));
    th.Start();
    Thread.Sleep(30 00);
    th.Abort();
    Thread.Sleep(10 00);

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Thread.Sleep

    JPSutor,

    I wouldn't use a polling mechanism like this. Rather, I would have some
    sort of notification on class which indicates when the property changes.
    The framework has a standard for things like this.

    The first (pre .NET 2.0) is to have an event on your class named
    <property name>Changed. This event is of type EventHandler, and you fire it
    when the event changes.

    If you are using .NET 2.0, you should use the INotifyProperty Changed
    interface to indicate when a property changed (it's more efficient in
    general than the old method).

    If you want to poll the property, then I would use a timer, instead of
    causing a thread to sleep, and check the property value there.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "JPSutor" <jpsutor@yahoo. com> wrote in message
    news:1135781991 .819733.188130@ o13g2000cwo.goo glegroups.com.. .[color=blue]
    > If you want to keep reading the value of a property (10 seconds) that
    > keeps changing,
    > how would you do it. I tried this, but it did not work
    >
    >
    > Thread th = new Thread(new ThreadStart(Rea dMe));
    > th.Start();
    > Thread.Sleep(30 00);
    > th.Abort();
    > Thread.Sleep(10 00);
    >[/color]


    Comment

    • MJB

      #3
      Re: Thread.Sleep


      I might also add that a while loop with a sleep duration works quite
      well. I noticed in pre .Net 2.0 the timer just wasn't reliable enough
      in certain situations. A while loop seemed to work well as an alternate
      solution.


      Nicholas Paldino [.NET/C# MVP] wrote:[color=blue]
      > JPSutor,
      >
      > I wouldn't use a polling mechanism like this. Rather, I would have some
      > sort of notification on class which indicates when the property changes.
      > The framework has a standard for things like this.
      >
      > The first (pre .NET 2.0) is to have an event on your class named
      > <property name>Changed. This event is of type EventHandler, and you fire it
      > when the event changes.
      >
      > If you are using .NET 2.0, you should use the INotifyProperty Changed
      > interface to indicate when a property changed (it's more efficient in
      > general than the old method).
      >
      > If you want to poll the property, then I would use a timer, instead of
      > causing a thread to sleep, and check the property value there.
      >
      > Hope this helps.
      >
      >[/color]

      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: Thread.Sleep

        Hi,


        [color=blue]
        >
        > Thread th = new Thread(new ThreadStart(Rea dMe));
        > th.Start();
        > Thread.Sleep(30 00);
        > th.Abort();
        > Thread.Sleep(10 00);[/color]


        This does not work cause Thread.Sleep refer to the current thread , the
        thread where the method is called, for this to work Thread.Sleep should be
        in the ReadMe() method.
        No need to call Abort btw.


        ITOH, I would not do it this way, I would use a timer .

        Or even better an event.




        --
        Ignacio Machin,
        ignacio.machin AT dot.state.fl.us
        Florida Department Of Transportation


        Comment

        • JPSutor

          #5
          Re: Thread.Sleep

          Nicholas,
          What I am trying to accomplish is this.
          I have a form with a progress bar on it.
          When I make a call to a method in another class to iterate through a
          recordset, I want the progress bar to show the progress.
          When I try referencing the form from my class I get an error message.
          What I'm trying to do here is to set a property value and read it in
          each time the timer fires. I realize this isn't the most effecient
          method, but my first try did not work

          Comment

          • JPSutor

            #6
            Re: Thread.Sleep

            Nicholas,
            What I am trying to accomplish is this.
            I have a form with a progress bar on it.
            When I make a call to a method in another class to iterate through a
            recordset, I want the progress bar to show the progress.
            When I try referencing the form from my class I get an error message.
            What I'm trying to do here is to set a property value and read it in
            each time the timer fires. I realize this isn't the most effecient
            method, but my first try did not work

            Comment

            • JPSutor

              #7
              Re: Thread.Sleep

              Nicholas,
              What I am trying to accomplish is this.
              I have a form with a progress bar on it.
              When I make a call to a method in another class to iterate through a
              recordset, I want the progress bar to show the progress.
              When I try referencing the form from my class I get an error message.
              What I'm trying to do here is to set a property value and read it in
              each time the timer fires. I realize this isn't the most effecient
              method, but my first try did not work

              Comment

              • JPSutor

                #8
                Re: Thread.Sleep

                Nicholas,
                What I am trying to accomplish is this.
                I have a form with a progress bar on it.
                When I make a call to a method in another class to iterate through a
                recordset, I want the progress bar to show the progress.
                When I try referencing the form from my class I get an error message.
                What I'm trying to do here is to set a property value and read it in
                each time the timer fires. I realize this isn't the most effecient
                method, but my first try did not work

                Comment

                • JPSutor

                  #9
                  Re: Thread.Sleep

                  Nicholas,
                  What I am trying to accomplish is this.
                  I have a form with a progress bar on it.
                  When I make a call to a method in another class to iterate through a
                  recordset, I want the progress bar to show the progress.
                  When I try referencing the form from my class I get an error message.
                  What I'm trying to do here is to set a property value and read it in
                  each time the timer fires. I realize this isn't the most effecient
                  method, but my first try did not work

                  Comment

                  • JPSutor

                    #10
                    Re: Thread.Sleep

                    Nicholas,
                    What I am trying to accomplish is this.
                    I have a form with a progress bar on it.
                    When I make a call to a method in another class to iterate through a
                    recordset, I want the progress bar to show the progress.
                    When I try referencing the form from my class I get an error message.
                    What I'm trying to do here is to set a property value and read it in
                    each time the timer fires. I realize this isn't the most effecient
                    method, but my first try did not work

                    Comment

                    • JPSutor

                      #11
                      Re: Thread.Sleep

                      Nicholas,
                      What I am trying to accomplish is this.
                      I have a form with a progress bar on it.
                      When I make a call to a method in another class to iterate through a
                      recordset, I want the progress bar to show the progress.
                      When I try referencing the form from my class I get an error message.
                      What I'm trying to do here is to set a property value and read it in
                      each time the timer fires. I realize this isn't the most effecient
                      method, but my first try did not work

                      Comment

                      • JPSutor

                        #12
                        Re: Thread.Sleep

                        Nicholas,
                        What I am trying to accomplish is this.
                        I have a form with a progress bar on it.
                        When I make a call to a method in another class to iterate through a
                        recordset, I want the progress bar to show the progress.
                        When I try referencing the form from my class I get an error message.
                        What I'm trying to do here is to set a property value and read it in
                        each time the timer fires. I realize this isn't the most effecient
                        method, but my first try did not work

                        Comment

                        • Luc Vaillant

                          #13
                          Re: Thread.Sleep

                          I suppose that the error message you've got is about thread synchronization .
                          If so, try this :

                          public class YourForm : System.Windows. Forms.Form
                          {
                          private delegate void ProgressBarDele gate(Int32 floodPercent);

                          public void UpdateProgressB ar(Int32 floodPercent)
                          {
                          // Need thread synchronization ?
                          //
                          if (this.InvokeReq uired)
                          {
                          this.Invoke(
                          new ProgressBarDele gate(this.Updat eProgressBar),
                          floodPercent);

                          return;
                          }

                          // Update ProgressBar flood percent here...
                          }
                          }

                          public class YourWorkingThre ad
                          {
                          private YourForm parentForm;
                          private DataTable dataToProceed;

                          YourWorkingThre ad(YourForm parentForm, DataTable dataToProceed)
                          {
                          this.parentForm = parentForm;
                          this.dataToProc eed = dataToProceed;
                          }

                          public void ThreadedJob()
                          {
                          Int32 rowCount = dataToProceed.R ows.Count;

                          for (Int32 i = 0; i < rowCount; i++)
                          {
                          // Proceed dataToProceed.R ows[i] ...

                          // Update progress bar
                          //
                          if (i % 100 == 0)
                          parentForm.Upda teProgressBar(( Int32)((Double) i / rowCount + 0.5));
                          }
                          }
                          }

                          "JPSutor" wrote:
                          [color=blue]
                          > Nicholas,
                          > What I am trying to accomplish is this.
                          > I have a form with a progress bar on it.
                          > When I make a call to a method in another class to iterate through a
                          > recordset, I want the progress bar to show the progress.
                          > When I try referencing the form from my class I get an error message.
                          > What I'm trying to do here is to set a property value and read it in
                          > each time the timer fires. I realize this isn't the most effecient
                          > method, but my first try did not work
                          >
                          >[/color]

                          Comment

                          Working...