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. A timer & a while function

A timer & a while function

Scheduled Pinned Locked Moved C#
helptutorialcareer
6 Posts 3 Posters 0 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
    danzar
    wrote on last edited by
    #1

    I am having a problem with a timer in my program. I am sure I am missing serveral things that someone else will see easly. this is a short example of my code. private void button_click_1(object sender, eventargs e) { starttimer(); Dothewhile(); } public void starttimer() { timer1.start(); timer1.interval = 1000; } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("hello"); } public void Dothewhile() { while (Run == true) { // do other stuff and when it detects its done it makes Run == false } } I am assuming the while statment is over taking the timer event and i just cant see when the timer hits but dont know for sure because i am stupid. But i would like to have some input on the ideal why the timer is never seen unless i turn the while statement off. thanks danzar Superman was my hero till he got a real job.

    S L 2 Replies Last reply
    0
    • D danzar

      I am having a problem with a timer in my program. I am sure I am missing serveral things that someone else will see easly. this is a short example of my code. private void button_click_1(object sender, eventargs e) { starttimer(); Dothewhile(); } public void starttimer() { timer1.start(); timer1.interval = 1000; } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("hello"); } public void Dothewhile() { while (Run == true) { // do other stuff and when it detects its done it makes Run == false } } I am assuming the while statment is over taking the timer event and i just cant see when the timer hits but dont know for sure because i am stupid. But i would like to have some input on the ideal why the timer is never seen unless i turn the while statement off. thanks danzar Superman was my hero till he got a real job.

      S Offline
      S Offline
      Skippums
      wrote on last edited by
      #2

      You are using a System.Windows.Forms timer, which ALWAYS runs on the main GUI thread. Therefore, your timer1_Tick method is waiting for the main thread to become available, which only happens when you exit the while loop. If you want to receive the Tick event in a separate thread (which you indicate is the desire from your post), then use either System.Timers.Timer or System.Threading.Timer, or start your method DoTheWhile in a new thread, which will free your main GUI thread for the timer. Jeff

      D 1 Reply Last reply
      0
      • D danzar

        I am having a problem with a timer in my program. I am sure I am missing serveral things that someone else will see easly. this is a short example of my code. private void button_click_1(object sender, eventargs e) { starttimer(); Dothewhile(); } public void starttimer() { timer1.start(); timer1.interval = 1000; } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("hello"); } public void Dothewhile() { while (Run == true) { // do other stuff and when it detects its done it makes Run == false } } I am assuming the while statment is over taking the timer event and i just cant see when the timer hits but dont know for sure because i am stupid. But i would like to have some input on the ideal why the timer is never seen unless i turn the while statement off. thanks danzar Superman was my hero till he got a real job.

        L Offline
        L Offline
        led mike
        wrote on last edited by
        #3

        danzar wrote:

        I am assuming the while statment is over taking the timer event

        #1 Don't make assumptions. #2 Don't make up terminology in text based communications ( over taking the timer ) because your readers won't know what you are talking about.

        D 1 Reply Last reply
        0
        • S Skippums

          You are using a System.Windows.Forms timer, which ALWAYS runs on the main GUI thread. Therefore, your timer1_Tick method is waiting for the main thread to become available, which only happens when you exit the while loop. If you want to receive the Tick event in a separate thread (which you indicate is the desire from your post), then use either System.Timers.Timer or System.Threading.Timer, or start your method DoTheWhile in a new thread, which will free your main GUI thread for the timer. Jeff

          D Offline
          D Offline
          danzar
          wrote on last edited by
          #4

          Perfect. thank you very much. this was exactly what i needed to know and a explaintion on why it wasnt working.. Thank you again!

          1 Reply Last reply
          0
          • L led mike

            danzar wrote:

            I am assuming the while statment is over taking the timer event

            #1 Don't make assumptions. #2 Don't make up terminology in text based communications ( over taking the timer ) because your readers won't know what you are talking about.

            D Offline
            D Offline
            danzar
            wrote on last edited by
            #5

            led mike wrote:

            danzar wrote: I am assuming the while statment is over taking the timer event #1 Don't make assumptions.

            I know its a bad habbit I have. sorry

            led mike wrote:

            #2 Don't make up terminology in text based communications ( over taking the timer ) because your readers won't know what you are talking about.

            My poor choice of words, was from the lack of not knowing the correct terminolgy for the issuse I was having. I know now that I was wrong in my description of the issuse I was having. Sorry again for the mistake but was the result of learning. A wise man said- You never learn unless you ask for help. :)

            L 1 Reply Last reply
            0
            • D danzar

              led mike wrote:

              danzar wrote: I am assuming the while statment is over taking the timer event #1 Don't make assumptions.

              I know its a bad habbit I have. sorry

              led mike wrote:

              #2 Don't make up terminology in text based communications ( over taking the timer ) because your readers won't know what you are talking about.

              My poor choice of words, was from the lack of not knowing the correct terminolgy for the issuse I was having. I know now that I was wrong in my description of the issuse I was having. Sorry again for the mistake but was the result of learning. A wise man said- You never learn unless you ask for help. :)

              L Offline
              L Offline
              led mike
              wrote on last edited by
              #6

              danzar wrote:

              I know its a bad habbit I have. sorry

              No need to apologize to me. My suggestions are offered so you can improve your situation.

              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