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
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) orConsole App
but the ideal to do this is create aservice
and running troughservices.msc
for ever.If you are in linux, I would do a
Console
and run it with acron task
.Then if you wanna still do your stuff in an
ASP.NET
like aWeb API
you can call from that service to the API withHttpClient
likeRestSharp Client
(you can get it from nuget), and then do the specific and finite task.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
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
And in your Controller/Service you need to receive the same instance of the cached object
you can use Quartz scheduler.
Quartz scheduler