1

I'm trying to figure out how to passively increase values in other running functions every second. (trying to increase the scores every second in a clicker game)

public int score=0;
public int sps=0;

private static Thread th = new Thread(new ThreadStart(Tick));
        public static void Tick()
        {
            score+=sps;
            System.Threading.Thread.Sleep(1000);
        }  

Update: Thanks for the help the ticking mechanism works now. The last thing I'm struggling with is setting the labelScore.Text=Convert.toString(score); since it's static. How could i get trough the static to be able to set my labelScore 's value?

  • 3
    Possible duplicate of [How do I run a simple bit of code in a new thread?](https://stackoverflow.com/questions/363377/how-do-i-run-a-simple-bit-of-code-in-a-new-thread) – Souvik Ghosh Jan 23 '18 at 08:33
  • 1
    You might be looking for a [System.Timers.Timer](https://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx) class. Read this: https://stackoverflow.com/questions/5727023/start-a-timer-from-different-thread-in-c-sharp – Fabjan Jan 23 '18 at 08:37
  • 1
    Make your `Tick()` method contain a loop, and then wherever you want to start the ticking just call `th.Start()`. You'll have to work out how you want to stop the thread but that's another story - @SouvikGhosh is right though, there is loads about this for you to read about on this site or on the web in general – LordWilmore Jan 23 '18 at 09:03
  • Thanks it worked. I've accidently public int-ed score and sps. I'm still not yet clear with private public static and stuff. :) – Levente Bánhidy Jan 24 '18 at 08:54

0 Answers0