c# - Keep process running forever -
this question has answer here:
i have dummy webisite want run task in while(true)
loop , want run forever. i'm in school project soccer bets , need (sort of) cloud website automatically update games , results me everyday, insert them in database , send emails, depending on eacher users bets (if gots points in game or not).
although, can't seem keep alive. 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 new asynchronous task task.factory.startnew(() => actualizacoes()); } public void actualizacoes() { bool verif = true; while (true) { // time in want task run task on method if (datetime.utcnow.tolocaltime().hour == 1 && datetime.utcnow.tolocaltime().minute == 30 && datetime.utcnow.tolocaltime().second == 0) verif = true; // quando voltar ficar online será actualizar. if (verif) { // want games 7 days datetime dt = new datetime(datetime.utcnow.tolocaltime().year, datetime.utcnow.tolocaltime().month, datetime.utcnow.tolocaltime().day + 7); // due providers limitation trial accounts thread.sleep(1000); // makes request tournaments api competicoes = sportradar.competicoes(); thread.sleep(1000); // makes request daily schedules api jogos = sportradar.jogosdodia(dt); // saves each game in each tournament 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; } } // want results day before dt = new datetime(datetime.utcnow.tolocaltime().year, datetime.utcnow.tolocaltime().month, datetime.utcnow.tolocaltime().day-1); thread.sleep(1000); // makes request daily results api jogos = sportradar.resultadosdodia(dt); // updates results on database , sends e-mails according points aquried 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 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 open page, active , go page load , run task. goes smoothly. on next day, don't updates database or emails unless open page again, not want.
how can achieved?
thanks help, in advance.
as mentioned in comment: should consider using service, or perhaps hang-fire is, according there website:
an easy way perform background processing in .net , .net core applications. no windows service or separate process required.
backed persistent storage. open , free commercial use.
there easy examples on there homepage, like:
var jobid = backgroundjob.schedule( () => console.writeline("delayed!"), timespan.fromdays(7));
or
//recurring cron recurringjob.addorupdate( () => console.writeline("recurring!"), cron.daily);
cron nice actually, scheduled tasks, see https://en.wikipedia.org/wiki/cron
note: if using hangfire, process created instance must alive execute tasks. although persistent storage saves tasks , execute them if delayed, if application not running, task not executed @ exact given moment.
in case, if app-pool idle or not running because has been recycled, task execute @ next warmup cycle of website, typically occurs when try access site.
if unwanted, can configure web-host (iis) keep application pool alive or immediate startup after recycling. "how to" depends bit on host using, it's same on every system.
more on matter:
iis https://technet.microsoft.com/en-us/library/cc753179(v=ws.10).aspx
or in worst case: how set iis keep application alive?
Comments
Post a Comment