locked
.NetCore Timer - Run Immediately, then start Timer RRS feed

  • Question

  • User1080340250 posted

    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
            }

    Friday, November 6, 2020 7:29 PM

All replies

  • User475983607 posted

    Just execute the method you wish to run in the Init() method.

            private void Init()
            {
                //Run the method here
    
                EventGenTimer = new Timer(30 * 1000)
                {
                    AutoReset = false
                };
                EventGenTimer.Elapsed += MockEventGenTimer_Elapsed;
            }

    Friday, November 6, 2020 7:48 PM
  • User1080340250 posted

    I had tried that ..

            private async void Init()
            {
                await Execute();
    
                EventGenTimer = new Timer(30 * 1000)
                {
                    AutoReset = false
                };
    
                EventGenTimer.Elapsed += MockEventGenTimer_Elapsed;
            }
    
            public void StartReceive()
            {
                EventGenTimer.Start();  --> System.NullReferenceException: 'Object reference not set to an instance of an object.'
    
                Console.WriteLine("Start receiving..........");
            }
    
            private async void MockEventGenTimer_Elapsed(object sender, ElapsedEventArgs e)
            {
                EventGenTimer.Enabled = false;
    
                if (ClientId > 0)
                {
                    await Execute();
                }
    
                EventGenTimer.Enabled = true;
            }
    
            private async Task Execute()
            {
    
                await Do AsyncCallHere();
    
            }



    Saturday, November 7, 2020 12:16 AM
  • User-474980206 posted

    That implies StartRecieve() is called before Init() has completed.

    Saturday, November 7, 2020 2:27 AM
  • User1080340250 posted

    This is in Program.cs 

       serviceContext.StartReceive();

    I modified things to look like this but it makes no difference has the call waits about 30 seconds before firing the first time.

            private async void Init()
            {
                EventGenTimer = new Timer(30 * 1000)
                {
                    AutoReset = false
                };
    
                EventGenTimer.Elapsed += MockEventGenTimer_Elapsed;
            }
    
            public async void StartReceive()
            { 
                await Execute();
    
                EventGenTimer.Start();
                Console.WriteLine("Start receiving..........");
            }
    
            private async void MockEventGenTimer_Elapsed(object sender, ElapsedEventArgs e)
            {
                EventGenTimer.Enabled = false;
    
                if (ClientId > 0)
                {
                    await Execute();
                }
    
                EventGenTimer.Enabled = true;
            }
    
            private async Task Execute()
            {
    
               await AsyncCallHere
            }

    Saturday, November 7, 2020 12:12 PM
  • User-474980206 posted

    Where is the code that calls Init()?

    Saturday, November 7, 2020 3:28 PM
  • User1080340250 posted

    This is the code in its entirety

    Program.cs
    
     using (var serviceScope = host.Services.CreateScope())
                {
                    var services = serviceScope.ServiceProvider;
    
                    try
                    {
                        var serviceContext = services.GetRequiredService<EventReceiverService>();
    
                        serviceContext.StartReceive();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Exception occured, message: {ex.Message}, stacktrace: {ex.StackTrace}");
                    }
                }
    public class EventReceiverService
        {
            public int ClientId = new int();
            private readonly IHubContext<EventHub> _eventHub;
            private Timer EventGenTimer;
    
            public EventReceiverService(IHubContext<EventHub> eventHub)
            {
                _eventHub = eventHub;
    
                Init();
            }
    
            private async void Init()
            {
                EventGenTimer = new Timer(30 * 1000)
                {
                    AutoReset = false
                };
    
                EventGenTimer.Elapsed += MockEventGenTimer_Elapsed;
            }
    
            public async void StartReceive()
            { 
                //await Execute();
    
                EventGenTimer.Start();
                Console.WriteLine("Start receiving..........");
            }
    
            private async void MockEventGenTimer_Elapsed(object sender, ElapsedEventArgs e)
            {
                EventGenTimer.Enabled = false;
    
                if (ClientId > 0)
                {
                    await Execute();
                }
    
                EventGenTimer.Enabled = true;
            }
    
            private async Task Execute()
            {
                await DoAnyAsycnThing();
            }
        }

    Saturday, November 7, 2020 6:02 PM
  • User711641945 posted

    Hi Tim,

    I modified things to look like this but it makes no difference has the call waits about 30 seconds before firing the first time.

    That is because you initialize the Init function in contributor,be sure remove the following code:

    public EventReceiverService(IHubContext<EventHub> eventHub)
    {
          _eventHub = eventHub;
          //Init();    //remove this...
    }

    Best Regards,

    Rena

    Monday, November 9, 2020 8:03 AM
  • User1080340250 posted

    Unfortunatly, when i comment out the init and enable await execute it get a huge error in Visual Studio saying System.private.corelib.pdb not load...it goes away as soon as i undo

            public EventReceiverService(IHubContext<EventHub> eventHub)
            {
                _eventHub = eventHub;
    
                //Init();
            }
    
            public async void StartReceive()
            { 
                await Execute();
    
                EventGenTimer.Start();
                Console.WriteLine("Start receiving..........");
            }

    Monday, November 9, 2020 1:52 PM
  • User711641945 posted

    Hi Tim,

    I can't reproduce your issue,please debug your code and check which line makes such error.Then share the code sample with us.

    Best Regards,

    Rena

    Tuesday, November 10, 2020 2:21 AM