I have a dummy webisite where I want to run a task in a while(true)
loop and I want it to run forever. I'm in a school project about soccer bets and I need this (sort of) cloud website to automatically update games and results for me everyday, insert them in the database and send emails, depending on eacher users bets (if they gots points in the game or not).
Although, I can't seem to keep it alive. This is my code:
private static string LigacaoBD = @"connectionString";
private static ApiMethods sportRadar = new ApiMethods();
private static Jogo jogo = new Jogo(LigacaoBD);
private static List<SportRadar.ClassesCliente.Jogo> jogos;
private static List<SportRadar.ClassesCliente.Competicao> competicoes;
protected void Page_Load(object sender, EventArgs e)
{
// Iniciates a new asynchronous task
Task.Factory.StartNew(() => Actualizacoes());
}
public void Actualizacoes()
{
bool verif = true;
while (true)
{
// Time in which I want the task to run the task on the method
if (DateTime.UtcNow.ToLocalTime().Hour == 1 && DateTime.UtcNow.ToLocalTime().Minute == 30 && DateTime.UtcNow.ToLocalTime().Second == 0)
verif = true;
// quando voltar a ficar online será actualizar.
if (verif)
{
// I want the games 7 days from now
DateTime dt = new DateTime(DateTime.UtcNow.ToLocalTime().Year, DateTime.UtcNow.ToLocalTime().Month, DateTime.UtcNow.ToLocalTime().Day + 7);
// This is due to the providers limitation to trial accounts
Thread.Sleep(1000);
// Makes the request for the tournaments to the API
competicoes = sportRadar.Competicoes();
Thread.Sleep(1000);
// Makes the request for the daily schedules to the API
jogos = sportRadar.JogosDoDia(dt);
// Saves each game in each tournament to the database
foreach (SportRadar.ClassesCliente.Jogo j in jogos)
{
foreach (SportRadar.ClassesCliente.Competicao c in competicoes)
if (j.Id_Torneio == c.Id)
{
jogo.Guardar(j.Clube_Casa, j.Clube_Visitante, c.Nome, c.Pais, j.Data);
break;
}
}
// I want the results from the day before
dt = new DateTime(DateTime.UtcNow.ToLocalTime().Year, DateTime.UtcNow.ToLocalTime().Month, DateTime.UtcNow.ToLocalTime().Day-1);
Thread.Sleep(1000);
// Makes the request for the daily results to the API
jogos = sportRadar.ResultadosDoDia(dt);
// Updates the results on the database and sends e-mails according to the points aquried but each user
foreach (SportRadar.ClassesCliente.Jogo j in jogos)
{
foreach (SportRadar.ClassesCliente.Competicao c in competicoes)
if (j.Id_Torneio == c.Id)
{
List<Utilizador> utilizadores = jogo.AlterarResultado(j.Clube_Casa, j.Clube_Visitante, c.Nome, c.Pais, j.Data, j.Golos_Casa, j.Golos_Visitante);
if (utilizadores.Count > 0)
{
foreach (Utilizador u in utilizadores)
Verificacoes.EnviarEmail("smtp.live.com", "betmagnum@hotmail.com", "Senha098!", u.Email,
"BetMagnum - Ganhou pontos num jogo!", "Parabéns, " + u.Nickname + ". Ganhou " + u.Pontuacao + " pontos no jogo " +
j.Clube_Casa + " - " + j.Clube_Visitante + ".<br/><br/>Resultado do jogo:<br/>" +
j.Clube_Casa + " " + j.Golos_Casa + " - " + j.Golos_Visitante + " " + j.Clube_Visitante + "<br/><br/>" +
"Pontos ganhos: "+u.Pontuacao, 587);
}
break;
}
}
verif = false;
}
Thread.Sleep(1000);
}
}
So, if I open the page, it will be active and will go to the page load and run the task. Everything goes smoothly. But on the next day, I don't get any updates to the database or emails unless I open the page again, which is not what I want.
How can this be achieved?
Thanks for the help, in advance.