Results 1 to 8 of 8

Thread: Is a timer the best way to listen for button presses or other events?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2015
    Posts
    132

    Is a timer the best way to listen for button presses or other events?

    It seems like it would be resource intensive to listen every tick.

    I have a GUI I made in VB.NET that edits an Access database. I emulate a centralized server (when there isn't one) by keeping a table in the database called users, where each user (a record is created for them if they don't already exist) running the program has an entry in this table for their Windows username.

    The table has a field called "Last Ping"

    I am setting up a timer for it to update this record for as long as they have the program open, every 5 seconds with the current DATE/TIME.

    I will then have another function attached to the timer that, every 10 seconds, will query the table for all users who have a ping within the last 5 seconds, and if they do, add their username to a list.

    That list will then be displayed on the main form of the program, and will refresh every 10 seconds along with the query event.

    Is this going to be a resource hog? Is there a more efficient way to do this? There is no way for me to run a server side copy of this app. Basically it sits on a share drive (the user doesnt have to install it, they just run the EXE out of the share drive), so a dozen users could all have the same application open from the same shared folder, which accesses a single Access database containing these tables.

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Is a timer the best way to listen for button presses or other events?

    It sounds like the end goal is for each instance of the app to have an up-to-date list of everyone who is running the app Correct?

    If so, then a UDP pipe solution may work better. Pipes are not my strength, so I can't post code without working it out. Essentially, when your app starts or closes, it broadcasts UDP packet on the network that your app would also listen for. If you receive a packet, you update the list with information in the packet (who is joining, or who is leaving).
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Is a timer the best way to listen for button presses or other events?

    I was worried by the thread title, but it sounds as if you are only concerned about writing and reading data in a small database table.

    Given that it is only once every 5 seconds, and the table sounds like it will be very small (under 1000 records), there shouldn't be any issues with resources etc. In terms of efficiency I don't think its worth worrying about better options (unless they are easier for you to write), as the method you are thinking of sounds like it will be fine.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,990

    Re: Is a timer the best way to listen for button presses or other events?

    I agree with both comments. Doing a query every five seconds will be trivial as far as resources, but I've done something similar to this using UDP.

    To explain the UDP solution a bit more, I'd say this: The UDP protocol is connectionless, light, and fast. It was used for LAN games for this reason. TCP is like a phone call, in that one end calls the other end, and talking only happens if somebody picks up. UDP is like sending a postcard. It doesn't matter whether there is anybody at the address listening. UDP broadcast over a LAN is like sending a spam advertisement, except that it doesn't have those negative connotations of spam. Because UDP is small, fast, and can broadcast easily, it is favored for sending little notes between systems.

    I have a program that could have multiple installations at a single facility. In my case, any program could make changes to a database, and all the other installations will need to know to refresh their displays. Therefore, I used the UDP class that I have posted in the .NET CodeBank as the basis of a little UDP net. I send pings out onto the network from each system every couple seconds (three seconds, I think) from a background thread. All the application instances are pinging every few seconds, and all the application instances are listening for other pings. The data I'm sending includes a GUID application ID and the datetime when the message was sent. All the listeners note the GUIDs they are receiving (ignoring their own), and the time of the message. Therefore, I have a dictionary (of GUID, Date) such that I can see the latest date that a message was received from each GUID, which means from each installation.

    I don't know whether this approach is better or worse than your timer solution, but it's a different option.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2015
    Posts
    132

    Re: Is a timer the best way to listen for button presses or other events?

    Quote Originally Posted by Shaggy Hiker View Post
    I agree with both comments. Doing a query every five seconds will be trivial as far as resources, but I've done something similar to this using UDP.

    To explain the UDP solution a bit more, I'd say this: The UDP protocol is connectionless, light, and fast. It was used for LAN games for this reason. TCP is like a phone call, in that one end calls the other end, and talking only happens if somebody picks up. UDP is like sending a postcard. It doesn't matter whether there is anybody at the address listening. UDP broadcast over a LAN is like sending a spam advertisement, except that it doesn't have those negative connotations of spam. Because UDP is small, fast, and can broadcast easily, it is favored for sending little notes between systems.

    I have a program that could have multiple installations at a single facility. In my case, any program could make changes to a database, and all the other installations will need to know to refresh their displays. Therefore, I used the UDP class that I have posted in the .NET CodeBank as the basis of a little UDP net. I send pings out onto the network from each system every couple seconds (three seconds, I think) from a background thread. All the application instances are pinging every few seconds, and all the application instances are listening for other pings. The data I'm sending includes a GUID application ID and the datetime when the message was sent. All the listeners note the GUIDs they are receiving (ignoring their own), and the time of the message. Therefore, I have a dictionary (of GUID, Date) such that I can see the latest date that a message was received from each GUID, which means from each installation.

    I don't know whether this approach is better or worse than your timer solution, but it's a different option.

    I don't think UDP would work because there is not always a copy of the program running. The program only exists when someone has it open, so if you are the only user it has no server app to communicate with. When two users are online, they see eachother via the database, as the form returns anyone active in the last few seconds as "online".

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,990

    Re: Is a timer the best way to listen for button presses or other events?

    UDP doesn't use a server app, that's the whole point of it being connectionless. The old LAN games were always peer-to-peer, as is the program I mentioned. If there is only one running, it is sitting there pinging off into the ether. It won't ever receive a ping because nobody else is pinging. If a second instance is started, then the two will be receiving pings from each other, and so on. They never establish connections. They are just sending off signals in case somebody happens to be listening.

    However, a UDP solution will have some issues if the computers aren't on the same LAN, because most UDP communication is blocked by default via firewalls. That's no big deal for internal communication on a LAN, but a bit harder off the LAN.
    My usual boring signature: Nothing

  7. #7
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Is a timer the best way to listen for button presses or other events?

    This does sound confusing, but, If I can interpret what's wanted, I'd suggest:

    1. Use the timer to update the timestamp for the current user of the application; If the program isn't running, the database isn't updating.

    Now, it sounds like there's a second requirement from the thread title: even though the application is open, they may not be 'active', so:

    2. Every time an 'activity' occurs on the form, update an internal timestamp in the application. Send this 'activity' timestamp along with the 'application open' timestamp.

    This would be nowhere near a resource hog for the clients or server, but if you have 'thousands' of users, then you are really stressing the Access database, and should probably be using something more robust.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2015
    Posts
    132

    Re: Is a timer the best way to listen for button presses or other events?

    I got it working on my first try, works as follows:


    1. Checks if your windows username is on the list
    2. Creates username in database if doesn't already exist
    3. Pings the current system time every 5 seconds
    4. Pulls a list of all users with a ping in the last 5 seconds on the usertable to a datatable
    5. Binds the datatable to a listview that refreshes every 5 seconds

    Worked flawlessly!

    I do not think a ping would work due to the configuration of the network.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width