Timer not working in thread

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sony.m.2007@googlemail.com

    Timer not working in thread

    Hi,
    My application processes a file and updates the data into sql server.
    The process takes around 30-90 seconds.I decided to put a timer to
    display the user a label(making label visible false and true) with
    message like "processing ..." and do the other process using thread.
    The timer won't works when the thread is started, so the label is not
    displayed in the form
    I have a label label1,a timer timer1 and command button in a form
    timer1.interval =100;
    private void timer1_Tick(obj ect sender, System.EventArg s e)
    {
    label1.Visible = ! label1.Visible;
    }

    private void commandButton_C lick(object sender, System.EventArg s e)
    {
    timer1.enabled= true;
    label1.visible= true;
    this.refresh
    Thread t=new thread(new threadstart(upd ateMethod))
    t.start()
    timer1.enabled= false;
    label1.visible= false
    }

    When I execute the above code label is not displayed in the
    form.After the completion of thread only label get displayed.
    Is there anything wrong in the above method of blinking a label?

    Cheers,
    Sony
  • Marc Gravell

    #2
    Re: Timer not working in thread

    You are disabling it too soon... when you disable the timer, I doubt
    that updateMethod has even begun... you need to disable this at the end
    of updateMethod instead, first jumping back onto the UI thread (due to
    thread affinity):

    void updateMethod() {
    ... your code
    this.Invoke((Me thodInvoker)del egate {
    timer1.enabled= false;
    label1.visible= false;
    });
    }

    Marc

    Comment

    • Marc Gravell

      #3
      Re: Timer not working in thread

      Ooh - just thought of a very useful extension method to Form:

      public static void Invoke(this Form form, Action action) {
      if(form == null) throw new ArgumentNullExc eption("form");
      if(action == null) throw new ArgumentNullExc eption("action" );

      form.Invoke((De legate)action);
      }

      Then you can use:

      this.Invoke(del egate {...});
      or
      this.Invoke(() ={...});

      Not a huge thing, but I like it ;-p

      Marc

      Comment

      • Marc Gravell

        #4
        Re: Timer not working in thread

        With Join you are simply hanging the UI; see my other post. And I don't
        think Jon was suggesting posting that code - just something that
        compiled and demonstrated the problem! You'd be amazed at how often we
        get questions where the example code (because the OP hasn't tried it)
        either doesn't show the same problem, or has even bigger issues [or just
        doesn't compile].

        Marc

        Comment

        • Marc Gravell

          #5
          Re: Timer not working in thread

          (actually the target could have been Control or ISynchronizeInv oke)

          Comment

          • Marc Gravell

            #6
            Re: Timer not working in thread

            I'm getting copiler errors

            Which version of .NET are you using? 2.0 and above should be fine (if
            you add a semicolon and fix the case that was copied verbatim from your
            post). If you are using 1.1 (VS2003), then you need to do something
            slightly different [I can't check if it compiles as I don't have 1.1 nearby]

            Marc

            void UpdateMethod()
            {
            //...
            this.Invoke(new MethodInvoker(D isableTimer));
            }
            void DisableTimer()
            {
            timer1.Enabled = false;
            label1.Visible = false;
            }

            Comment

            • Marc Gravell

              #7
              Re: Timer not working in thread

              Then there's the question of whether you're using C# 3 to start with...
              Actually he cited the C# 2 version - so my guess is C# 1.2
              (see, I did read the book!)

              Marc

              Comment

              • Ignacio Machin ( .NET/ C# MVP )

                #8
                Re: Timer not working in thread

                I have a label label1,a timer  timer1 and command button in a form
                timer1.interval =100;
                private void timer1_Tick(obj ect sender, System.EventArg s e)
                                {
                                        label1.Visible = ! label1.Visible;
                                }
                Are you showing/hidding the label multiple times????
                private void commandButton_C lick(object sender, System.EventArg s e)
                {
                timer1.enabled= true;
                label1.visible= true;
                this.refresh
                Thread t=new thread(new threadstart(upd ateMethod))
                t.start()
                timer1.enabled= false;
                label1.visible= false
                The last line of the above code is the line of the problem, you are
                disabling the timer right there, most probably before it might even
                tick for the first time.
                You have to disable as the end of the worker method ( UpdateMethod)

                Comment

                • sony.m.2007@googlemail.com

                  #9
                  Re: Timer not working in thread

                  On Apr 4, 3:45 pm, "Ignacio Machin ( .NET/ C# MVP )"
                  <ignacio.mac... @gmail.comwrote :
                  I have a label label1,a timer timer1 and command button in a form
                  timer1.interval =100;
                  private void timer1_Tick(obj ect sender, System.EventArg s e)
                  {
                  label1.Visible = ! label1.Visible;
                  }
                  >
                  Are you showing/hidding the label multiple times????
                  >
                  private void commandButton_C lick(object sender, System.EventArg s e)
                  {
                  timer1.enabled= true;
                  label1.visible= true;
                  this.refresh
                  Thread t=new thread(new threadstart(upd ateMethod))
                  t.start()
                  timer1.enabled= false;
                  label1.visible= false
                  >
                  The last line of the above code is the line of the problem, you are
                  disabling the timer right there, most probably before it might even
                  tick for the first time.
                  You have to disable as the end of the worker method ( UpdateMethod)
                  Yes showing/hidding the label multiple times


                  Cheers,
                  Sony

                  Comment

                  Working...