I am making a clicker game in visual studio C#.NET Windows Forms App. I need a game loop that runs once every second, to handle the money over time. Here is the code I have tried enter image description here
Asked
Active
Viewed 133 times
-1
-
look at [this](https://stackoverflow.com/questions/6169288/execute-specified-function-every-x-seconds) post – João Paulo Amorim Feb 13 '19 at 17:30
-
My guess is that your game does "Crash", but it does "Hang". There's a big difference. – Flydog57 Feb 13 '19 at 17:31
-
1It would be useful if you could show the code you have tried so far & also give details of what platform you are using - e.g. WPF or WinForms, etc - please edit the question rather than add as a comment – PaulF Feb 13 '19 at 17:33
-
1How exact does that "once a second" have to be? Are you, for example, running a clock (so that a small error will accumulate into a large error over time)? Or do you just have a spec that says "update some UI element once a second"? – Flydog57 Feb 13 '19 at 17:33
-
Possible duplicate of [how can i make an infinite loop with 5 second pauses](https://stackoverflow.com/questions/11632419/how-can-i-make-an-infinite-loop-with-5-second-pauses) – Hoshani Feb 13 '19 at 17:37
-
Note you would only need to do this if you needed to update a UI element in realtime, otherwise you can just calculate it on-demand from a stored time stamp. – Alex K. Feb 13 '19 at 17:46
-
1If you're wondering why you're getting pushback here in the form of comments, downvotes and close votes, answers can be found at https://idownvotedbecau.se/noresearch https://idownvotedbecau.se/imageofcode. Also, you seem to be an extreme beginner programmer but what you're wanting to do is going to require some more advanced knowledge, specifically how threading works in a winforms application. You should read a book on writing winforms applications before you do anything else, it will save you a ton of effort and frustration. – Feb 13 '19 at 18:07
-
Firstly, you will find that you will get downvoted if you add code as images - quite often people here will try to help by trying to repeat your problem - if the code is added as part of the question, we can cut and paste - we are very unlikely to try to copy code from an image. Secondly, a snippet of code out of context does little to help us - we need to know where in the your code that snippet is placed. Lastly, it is likely wherever that code is placed it is blocking the main GUI thread, hence your application appears to hang - take Will's advice & find out about Winforms Threading. – PaulF Feb 13 '19 at 18:40
1 Answers
1
You should really look into setting up a game loop if you're writing a game. They're used to control the whole flow of the game, check states, and update values and UI. That would really help you. One of the biggest parts of a game loop is updating time-based actions, which is what it sounds like you're trying to do. Check out this link for an introduction on game loops.
If you were using a game loop, you could check in your loop to see if one second had passed since you added the last cash, and if so, add another cash. Game loops are where it's at! Good luck

Malcolm G
- 504
- 5
- 11
-
If you think this is a suitable solution, then what would be of more help to the OP would be an example of how to implement a game loop in a Winforms application. As it stands this is more appropriate as a comment rather than an answer. – PaulF Feb 13 '19 at 18:46