简体   繁体   English

如何使用c#asp.net MVC每10秒调用一次方法?

[英]How to call a Method every 10 seconds with c# asp.net MVC?

Im stuck trying to call a method every 10 seconds, I read about timers and threads but the problem is that i do not know where to put the method to be fired, MVC projects have a Main? 我停留试图每10秒调用一个方法,我读到了有关计时器和线程的信息,但问题是我不知道将方法激发到哪里,MVC项目有一个Main?

    private async Task ActualizarPrecios()
      {
        Entities model = new Entities();
        var transaciones = model.Transacciones.Where(a => a.ESTADO == 
         true).ToList();
        string Url = ObtenerUrl(model);

        ApiForex.IniciarCliente();

        ProcesarRequest procesar = new ProcesarRequest();
        JObject divisas = await procesar.CargarJson(Url);

        foreach(Transacciones item in transaciones)
        {
          item.PRECIO_ACTUAL = (string)divisas["rates"][item.DIVISA] 
        ["rate"];
        }
        model.SaveChanges();
     }

The code that is to upload some columns of tables in the database. 用于上载数据库中表的某些列的代码。 I dont know how to fire this method every x seconds 我不知道如何每x秒触发此方法

You might want to investigate scheduling APIs and third party tools. 您可能要研究调度API和第三方工具。 Hangfire ( https://www.hangfire.io/ ) is one that I have used successfully Hangfire( https://www.hangfire.io/ )是我成功使用的一种

I would use JavaScript to achieve this. 我将使用JavaScript实现这一目标。

At the bottom of your View file (*.cshtml) add this code: 在您的View文件(* .cshtml)的底部添加以下代码:

<script type="text/javascript">
 var interval = 10000; 
 setInterval(function() { Update() }, interval);

 function Update(){
     $.get("/[ControllerName]/ActualizarPrecios", function(){
         alert( "Load was performed.")}
     );
 } 
</script>

Explanation: 说明:

<script type="text/javascript">

This is HTML denodes you are writing scripts in the JavaScript language 这是您使用JavaScript语言编写脚本的HTML节点

var interval = 10000; setInterval(function() { Update() }, interval);

Sets the interval to 10 seconds (in milliseconds) 将时间间隔设置为10秒(以毫秒为单位)

Then uses the JavaScript function for repeating executions on an interval see more on W3 Schools 然后使用JavaScript函数按间隔重复执行,请参阅W3 Schools中的更多内容

function Update(){
     $.get("/[ControllerName]/ActualizarPrecios", function(){
         alert( "Load was performed.")}
     );
 } 

This function uses Ajax in jQuery to call an API. 此函数在jQuery中使用Ajax来调用API。 Which exists in your controller. 哪个存在于您的控制器中。 You haven't shared your controller name so replace the code accordingly, the method looks like a get method to me however it is post then change $.get to $.post . 您尚未共享控制器名称,因此请相应地替换代码,该方法对我来说似乎是get方法,但是它是post然后将$.get更改为$.post The function after the url path is executed on callback to your request. url路径后的函数在对您的请求的回调中执行。 For now, to make it clear during testing it is running the line alert( "Load was performed.") which will pop up a dialog box in your browser; 现在,为了使其在测试过程中清晰可见,它正在运行行alert( "Load was performed.") ,该alert( "Load was performed.")将在浏览器中弹出一个对话框。 if you want to write something out to the html after running the request you can do that here. 如果您想在运行请求后将某些内容写到html中,则可以在此处进行操作。

*Note jQuery is included out the box in MVC C# projects. *请注意,jQuery已包含在MVC C#项目中。 In your \\shared\\_Layout.cshtml file you'll see it reference in a section at the bottom of the page. \\shared\\_Layout.cshtml文件中,您将在页面底部的部分中看到它的引用。

1) If your application is going to host on Server or virtual machine (VM) then you can create window service and register service on the same. 1)如果您的应用程序要托管在服务器或虚拟机(VM)上,则可以在其上创建窗口服务和注册服务。

Url : https://www.aspsnippets.com/Articles/Simple-Windows-Service-that-runs-periodically-and-once-a-day-at-specific-time-using-C-and-VBNet.aspx 网址: https : //www.aspsnippets.com/Articles/Simple-Windows-Service-that-persiodicically-and-once-a-day-at-specific-time-using-C-and-VBNet.aspx

2) If your application is going to host on Azure pass then you can use Azure web jobs for the same. 2)如果您的应用程序要托管在Azure pass上,则可以使用Azure web作业。 Url : https://docs.microsoft.com/en-us/azure/app-service/webjobs-create 网址: https//docs.microsoft.com/zh-CN/azure/app-service/webjobs-create

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM