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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. waiting but still recieving messages....is there a way?

waiting but still recieving messages....is there a way?

Scheduled Pinned Locked Moved C#
helpquestioncsharp
4 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.
  • N Offline
    N Offline
    nick158
    wrote on last edited by
    #1

    I only started coding c# a week ago, and I’m a little stuck! I'm making an application where the user has 1 second to click the mouse button once they see movement on the screen. My problem is: what is the best way of waiting until the mouse button has been pressed without hogging the system resources? If I use a sleep then the onclick method for the mouse click won’t be called until the sleep has finished I assume? I need the time at which the mouse button was pressed, therefore sleeping for 1 second and then seeing if the mouse was pressed isn't good enough. Currently the application was coded by someone else, and it uses a while loop until the time equals 1 second later. Obviously this allows the application to receive messages and make the onclick method call occur but it also uses all the processing resources. Further to this, is there a way of getting a time stamp of when a message occurred? This would be more accurate than getting the time once the on click method has been called. Any help with this would be greatly appreciated! Nick

    R G 2 Replies Last reply
    0
    • N nick158

      I only started coding c# a week ago, and I’m a little stuck! I'm making an application where the user has 1 second to click the mouse button once they see movement on the screen. My problem is: what is the best way of waiting until the mouse button has been pressed without hogging the system resources? If I use a sleep then the onclick method for the mouse click won’t be called until the sleep has finished I assume? I need the time at which the mouse button was pressed, therefore sleeping for 1 second and then seeing if the mouse was pressed isn't good enough. Currently the application was coded by someone else, and it uses a while loop until the time equals 1 second later. Obviously this allows the application to receive messages and make the onclick method call occur but it also uses all the processing resources. Further to this, is there a way of getting a time stamp of when a message occurred? This would be more accurate than getting the time once the on click method has been called. Any help with this would be greatly appreciated! Nick

      R Offline
      R Offline
      Robin Panther
      wrote on last edited by
      #2

      If I've understood you correctly, then I suggest you something like this: remember when timer was started in absolute values. use another thread loop with Join(interval) method, where interval = precision of time measurements, that you need. You can use timer component in the form to implement this simply. On each timer tick - check, is it second already. onClick event must first remember what time is now, in absolute values, stop timer, then calculate difference between start and stop time. Should work. Robin Panther

      N 1 Reply Last reply
      0
      • R Robin Panther

        If I've understood you correctly, then I suggest you something like this: remember when timer was started in absolute values. use another thread loop with Join(interval) method, where interval = precision of time measurements, that you need. You can use timer component in the form to implement this simply. On each timer tick - check, is it second already. onClick event must first remember what time is now, in absolute values, stop timer, then calculate difference between start and stop time. Should work. Robin Panther

        N Offline
        N Offline
        nick158
        wrote on last edited by
        #3

        Thanks Robin, I was thinking of something like that, I just wasn't sure if there was something similar to the sleep method which allowed messages to pass. I'll have a go at implementing it that way. Many thanks Nick

        1 Reply Last reply
        0
        • N nick158

          I only started coding c# a week ago, and I’m a little stuck! I'm making an application where the user has 1 second to click the mouse button once they see movement on the screen. My problem is: what is the best way of waiting until the mouse button has been pressed without hogging the system resources? If I use a sleep then the onclick method for the mouse click won’t be called until the sleep has finished I assume? I need the time at which the mouse button was pressed, therefore sleeping for 1 second and then seeing if the mouse was pressed isn't good enough. Currently the application was coded by someone else, and it uses a while loop until the time equals 1 second later. Obviously this allows the application to receive messages and make the onclick method call occur but it also uses all the processing resources. Further to this, is there a way of getting a time stamp of when a message occurred? This would be more accurate than getting the time once the on click method has been called. Any help with this would be greatly appreciated! Nick

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          Put a timer on the form that fires after a second. Then you just have to sit back and wait for events, and see if the click or tick fires first. --- b { font-weight: normal; }

          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