❔ Timer

Is it possible to add a timer that always counts down no matter where i am in the program ?
29 Replies
Lucky_Pepsi_best
I want a timer in my code to end the game after 2 mins has gone by (if it is can you not say the code but point me in the direction so i can work it ou tmyself) i use visual studio
Jester
Jester8mo ago
yeah but which kind of project
Dropps
Dropps8mo ago
well mind explaining the problem a bit more detailed? what technologies are you using? unity? wpf? maui? blazor?
Lucky_Pepsi_best
in visual studio its a game and iw ant it to end after 2 mins
Dropps
Dropps8mo ago
visual studio is an ide
Lucky_Pepsi_best
so is it possible ?
Jester
Jester8mo ago
yes
EntireTeamIsBabies
So. Do you know a bit of you way around DotNET? or are you just starting out and curious of what C# can do? Since you're not responding, maybe are asleep or otherwise occupied I will Give you a few demonstrations of C# Programs on Date, Time & Year, To help you on your way dealing with time in C#
EntireTeamIsBabies
https://learn.microsoft.com/en-us/dotnet/api/system.datetime?view=net-7.0 This is a documentation page dealing with Time for C#/dotNET
DateTime Struct (System)
Represents an instant in time, typically expressed as a date and time of day.
EntireTeamIsBabies
Crudely, We could get Date and time thusly:
using System;
class Program
{
static void Main()
{
TimeZone zone = TimeZone.CurrentTimeZone;
DateTime local = zone.ToLocalTime(DateTime.Now);
Console.WriteLine("The Local Time is : {0}",local);
Console.ReadLine();
}
}
using System;
class Program
{
static void Main()
{
TimeZone zone = TimeZone.CurrentTimeZone;
DateTime local = zone.ToLocalTime(DateTime.Now);
Console.WriteLine("The Local Time is : {0}",local);
Console.ReadLine();
}
}
Lucky_Pepsi_best
i just wanna know if its possible as it would require going through something seperate to the main program while the main program running
EntireTeamIsBabies
I have to forewarn you I am close to ending my day here but I can give you a bump over the hill, is that good to you? I'm not spoonfeeding you the answers but I can help you out with showing some work. Good? First and foremost, Yes I think it is workable. But I think you are gonna have to deal with threading next to Daytimefunctionality as well. Which is a bit rough for a starter. Like, crudely,
// A couple of variable instances.
private int _countDown = 120; // Seconds
private Timer _timer;

// constructor or load event
_timer = new Timer();
_timer.Tick += new EventHandler(timer_Tick);
_timer.Interval = 1000;
_timer.Start();

//event handler
void timer_Tick(object sender, EventArgs e)
{
_countDown--;
if (_countDown < 1)
{
//120 for 120 seconds, IE 2 minutes
_countDown = 120;
}
lblCountDown.Text = _countDown.ToString();
}
// A couple of variable instances.
private int _countDown = 120; // Seconds
private Timer _timer;

// constructor or load event
_timer = new Timer();
_timer.Tick += new EventHandler(timer_Tick);
_timer.Interval = 1000;
_timer.Start();

//event handler
void timer_Tick(object sender, EventArgs e)
{
_countDown--;
if (_countDown < 1)
{
//120 for 120 seconds, IE 2 minutes
_countDown = 120;
}
lblCountDown.Text = _countDown.ToString();
}
This would be a countdown timer for 120 seconds. (2 minutes) But that timer has to "Click" some other function, right? Otherwise this program runs and shuts after having count down without no one any the wiser. listen, I want to help out, but you are hardly giving me any responses to my questions which is kind of killing communication for me.
Lucky_Pepsi_best
would that run no matter what line the pogram is on
EntireTeamIsBabies
It should run no matter what, unless your surrounding code dictates other standards.
Lucky_Pepsi_best
what ya mean by dicated other standards ?
EntireTeamIsBabies
if some other code says: no this private int _countDown = Completelysomethingelse or a million other things. It just means to say, if this is nearly the only thing in main() then it should be fine. If you can see something that is "conditioning" or "leveraging" the datetime struct otherwise, you get different results. I'm sorry for the wordy-ness, but that's part of the territory. Code = language
EntireTeamIsBabies
-- Please read the contents of this link https://learn.microsoft.com/en-us/dotnet/api/system.datetime?view=net-7.0 It explains a lot about using dates, time and years in C#
DateTime Struct (System)
Represents an instant in time, typically expressed as a date and time of day.
Lucky_Pepsi_best
k
EntireTeamIsBabies
One simple instance:
using System;
class Program
{
static void Main()
{
TimeZone zone = TimeZone.CurrentTimeZone;
DateTime local = zone.ToLocalTime(DateTime.Now);
Console.WriteLine("The Local Time is : {0}",local);
Console.ReadLine();
}
}
using System;
class Program
{
static void Main()
{
TimeZone zone = TimeZone.CurrentTimeZone;
DateTime local = zone.ToLocalTime(DateTime.Now);
Console.WriteLine("The Local Time is : {0}",local);
Console.ReadLine();
}
}
this gets the local time. (I'm letting you read quietly in a bit, but I think You are running to fast for your boots) Get the gist of date-time first, then after, learn to condition it. take your time.
Lucky_Pepsi_best
if the only thing using time is the countdown and i have a Thread.Sleep(50) it should be fine ? its 1 am so im tired so not rlly taking in much tmr ill come back through and laern it all are you good at coding c# ? cause i have this lgoic error that i just cant fix no matter what i try
EntireTeamIsBabies
I am as good as you are. I may just have fixed more things with carefully reading along the way.
Lucky_Pepsi_best
can you take a look at my error and see if you can fix ?
EntireTeamIsBabies
I have to be honest and say no. I can look into the code but before I know how to fix it I am 2 hours into the future. It would be a waste of both our sleep. So. Can we pick this up again tomorrow?
Lucky_Pepsi_best
sure ill send the code and the error so if you got free time ya can look at it tmr
Lucky_Pepsi_best
https://paste.mod.gg/ygzzctyynomz/0 after pressing enter then spcea and hold space it registers 2 input but i just wnat it to take 1 input and then it wont take another input until the spacebar has been releasaed annd repressed
BlazeBin - ygzzctyynomz
A tool for sharing your source code with the world!
Lucky_Pepsi_best
in the 3rd like it says private Timer - is timer a data type ? also whats teh event handler ? as in waht does it mean by eventhandler ping me when your on
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Lucky_Pepsi_best
@OCT'🥚EntireTeamIsBabies!
Accord
Accord8mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.