skip to Main Content

I want to run a method which will always running like:

public async Task GetMessageUpdatesAsync()
{    
    while(true)
    {
        await GetUpdatesAsync();
    }
}

I want to run it in separate process. while my app continue run.

How ca I do it?

Update:

here is my method code

    public static async Task<string> GetUpdatesAsync()
    {
        string telegramToken = "Token";
        var botClient = new TelegramBotClient(telegramToken);
        int? updateId=null;
        while (true) // this will not break
        {
           var updates = await botClient.GetUpdatesAsync(updateId);
           foreach(var update in updates)
           {
               //do some thing
               updateId = update.Id + 1;
           }
        }
    }

I want previous method running while other website code working (like get information from database, update database information …).

  • List item

3

Answers


  1. An ASP.NET (web app) is not made to work for ever. Is only to work with a quick response to some request.

    You should create two projects to do this. There is no work or improvement added do it in the same project.

    If you are in windows, you can use a WinForm (desktop app) or Console App but the ideal to do this is create a service and running trough services.msc for ever.

    If you are in linux, I would do a Console and run it with a cron task.

    Then if you wanna still do your stuff in an ASP.NET like a Web API you can call from that service to the API with HttpClient like RestSharp Client (you can get it from nuget), and then do the specific and finite task.

    Login or Signup to reply.
  2. You can create a hosted service to run as a background service and updates the value while the api is running

    Create a backgroundService Class:

    Worker.cs

        public class Worker:BackgroundService
        {
            private readonly IList<string> _list;
    
            public Worker(IList<string> list)
            {
                _list = list;
            }
            protected override async Task ExecuteAsync(CancellationToken stoppingToken)
            {
                while (!stoppingToken.IsCancellationRequested)
                    await GetUpdatesAsync();
            }
    
            private async Task GetUpdatesAsync()
            {
                _list.Add(Guid.NewGuid().ToString());
                //Delay in milliseconds between each update
                await Task.Delay(1000);
            }
        }
    

    In your Startup.cs add the hosted service and a singleton instance that will be updated by it, in this example we are modifing an List

    //...rest of startup
    
    services.AddSingleton<IList<string>, List<string>>();
    services.AddHostedService<Worker>();
    

    And in your Controller/Service you need to receive the same instance of the cached object

    Login or Signup to reply.
  3. you can use Quartz scheduler.

    Quartz scheduler

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search