Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Timer Controll in Thread

Timer Controll in Thread

Scheduled Pinned Locked Moved C#
8 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DeepOceans
    wrote on last edited by
    #1

    Hello I m developing a window application. I m doing diferent tasks against different events using multi-threading. Against 1 thread , I m enabling the timer, and against its function I m performing 1 task, but it is not working i.e it is not executing the timer function. if I put all the code against some button rather than within thread , then it works well. plz tell me how could I use the Timer within the thread. here is my Code. private void Start_Click(object sender, EventArgs e) { pthrd = new Thread(new ThreadStart(Thrd1)); pthrd .Start(); } void Thrd1() { MessageBox.Show("Thread Start"); timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("Start Grabbing"); Calculate(); }

    Shanzay

    G realJSOPR P 3 Replies Last reply
    0
    • D DeepOceans

      Hello I m developing a window application. I m doing diferent tasks against different events using multi-threading. Against 1 thread , I m enabling the timer, and against its function I m performing 1 task, but it is not working i.e it is not executing the timer function. if I put all the code against some button rather than within thread , then it works well. plz tell me how could I use the Timer within the thread. here is my Code. private void Start_Click(object sender, EventArgs e) { pthrd = new Thread(new ThreadStart(Thrd1)); pthrd .Start(); } void Thrd1() { MessageBox.Show("Thread Start"); timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("Start Grabbing"); Calculate(); }

      Shanzay

      G Offline
      G Offline
      Gareth H
      wrote on last edited by
      #2

      DeepOceans, I am not sure if this is correct, but i think its because the thread does the work and then exits, thus the timer will be told to start but then instantly quick, thus it never ticks. Since i am not sure if my thinking is correct, i dont have a solution other than to keep the thread alive. Whats the reason you want to run a Timer in the background anyways?

      Regards, Gareth. (FKA gareth111)

      D 1 Reply Last reply
      0
      • G Gareth H

        DeepOceans, I am not sure if this is correct, but i think its because the thread does the work and then exits, thus the timer will be told to start but then instantly quick, thus it never ticks. Since i am not sure if my thinking is correct, i dont have a solution other than to keep the thread alive. Whats the reason you want to run a Timer in the background anyways?

        Regards, Gareth. (FKA gareth111)

        D Offline
        D Offline
        DeepOceans
        wrote on last edited by
        #3

        I m using Timer to enable my function for grabbing the video frames on every tick. yes u might b right that thread does its work and exits but in my case it never actually goes in the function (i checked by printing a messegeBox). i already gave the code. plz try it and tell me more specific reason. Thanku so much !

        Shanzay

        1 Reply Last reply
        0
        • D DeepOceans

          Hello I m developing a window application. I m doing diferent tasks against different events using multi-threading. Against 1 thread , I m enabling the timer, and against its function I m performing 1 task, but it is not working i.e it is not executing the timer function. if I put all the code against some button rather than within thread , then it works well. plz tell me how could I use the Timer within the thread. here is my Code. private void Start_Click(object sender, EventArgs e) { pthrd = new Thread(new ThreadStart(Thrd1)); pthrd .Start(); } void Thrd1() { MessageBox.Show("Thread Start"); timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("Start Grabbing"); Calculate(); }

          Shanzay

          realJSOPR Online
          realJSOPR Online
          realJSOP
          wrote on last edited by
          #4

          Your thread function should look something like this:

          private void Thrd1()
          {
          // just sit and spin
          while (true)
          {
          // sleep for the desired time - this example uses 1 second
          Thread.Sleep(1000);

              Calculate();
          }
          

          }

          private void Calculate()
          {
          // do some work
          }

          BTW "Thrd1" is a crappy function name.

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          1 Reply Last reply
          0
          • D DeepOceans

            Hello I m developing a window application. I m doing diferent tasks against different events using multi-threading. Against 1 thread , I m enabling the timer, and against its function I m performing 1 task, but it is not working i.e it is not executing the timer function. if I put all the code against some button rather than within thread , then it works well. plz tell me how could I use the Timer within the thread. here is my Code. private void Start_Click(object sender, EventArgs e) { pthrd = new Thread(new ThreadStart(Thrd1)); pthrd .Start(); } void Thrd1() { MessageBox.Show("Thread Start"); timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("Start Grabbing"); Calculate(); }

            Shanzay

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            The Timer uses a thread, so you needn't use your own as well.

            realJSOPR 1 Reply Last reply
            0
            • P PIEBALDconsult

              The Timer uses a thread, so you needn't use your own as well.

              realJSOPR Online
              realJSOPR Online
              realJSOP
              wrote on last edited by
              #6

              But using a thread is more programmerish. The Timer class is for lazy people. :)

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              P 1 Reply Last reply
              0
              • realJSOPR realJSOP

                But using a thread is more programmerish. The Timer class is for lazy people. :)

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Certainly, but using both at once is nuts.

                realJSOPR 1 Reply Last reply
                0
                • P PIEBALDconsult

                  Certainly, but using both at once is nuts.

                  realJSOPR Online
                  realJSOPR Online
                  realJSOP
                  wrote on last edited by
                  #8

                  Agreed.

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups