Member
438 Points
328 Posts
Nov 06, 2020 07:29 PM|Tim Cadieux|LINK
Hi folkz, I'm still somewhat new to .netCore and am trying to determine how to use a Timer.
The below code works but it starts the timer, counts and then fires the function for the first time.
I'd like to change this in such a way that the function is run immediately, then start the timer.
any help would be appreciated
private Timer EventGenTimer; public EventReceiverService(IHubContext<EventHub> eventHub) { _eventHub = eventHub; Init(); } private void Init() { EventGenTimer = new Timer(30 * 1000) { AutoReset = false }; EventGenTimer.Elapsed += MockEventGenTimer_Elapsed; } public void StartReceive() { EventGenTimer.Start(); Console.WriteLine("Start receiving.........."); } private async void MockEventGenTimer_Elapsed(object sender, ElapsedEventArgs e) { EventGenTimer.Enabled = false; if (ClientId > 0) { await RunMe(); } EventGenTimer.Enabled = true; } private async Task runmeAsync() { // do domething Asynchere }
Member
438 Points
328 Posts
.NetCore Timer - Run Immediately, then start Timer
Nov 06, 2020 07:29 PM|Tim Cadieux|LINK
Hi folkz, I'm still somewhat new to .netCore and am trying to determine how to use a Timer.
The below code works but it starts the timer, counts and then fires the function for the first time.
I'd like to change this in such a way that the function is run immediately, then start the timer.
any help would be appreciated
***
Mark the replies as Answers if they answered your question.