在长期运行的过程中显示进度 [英] Show progress on long-running process

查看:69
本文介绍了在长期运行的过程中显示进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些服务器端进程可能需要很长时间才能运行(30-60m).这些是从我的Web服务器调用Web API并限制请求以使其与服务的最大请求速率相匹配的结果.

I have a few server-side processes that can take very long times to run (30-60m). These are a result of calling a web API from my webserver, and throttling the requests to match the maximum request rate of the service.

我希望能够在客户端上显示此进度,我该怎么做呢?我从哪里开始?

I want to be able to display the progress of this on the client, how can I go about doing this? Where do I start?

以前的答案暗示着SignalR,但这似乎与Asp.Net Core无关.

Previous answers have hinted at SignalR, but that does not seem to be a thing with Asp.Net Core.

看起来SignalR是带有Asp.Net核心的东西.在这种情况下,我有一个后续问题:

It looks like SignalR is a thing with Asp.Net core. As this is the case, I have a followup question:

我正在使用AngularJS,并通过对API端点的AJAX请求来管理客户端状态.尝试同时使用SignalR时是否存在任何潜在的编码或性能陷阱"?

I'm using AngularJS and managing the client-side state via AJAX requests to API endpoints. Are there any potential coding or performance "gotchas" when trying to also utilize SignalR in conjunction?

另外,还有没有将SignalR添加为依赖项的替代方法吗?看来它需要.Net Core的Beta版,我宁愿呆在稳定发行版中如果可能的话.

Additionally, are there alternatives that don't add SignalR as a dependency? It looks like it requires a beta-version of .Net Core, I'd rather stay in stable-release-land if at all possible.

推荐答案

您可以使用SignalR和

You can use SignalR and the IProgress interface to handle this. The basic implementation is something like this:

服务器端

[HubName("messageHub")]
public class MessageHub : Hub  // This is your server-side SignalR
{
    public async Task Countdown(IProgress<int> progress) // This is your IProgress
    {            
         // 60 second countdown for an example
         // NOT TESTED
         for (int i = 1; i <= 61; i += 1)
         {             
              await Task.Delay(1000);
              progress.Report(i);                    
         }
     }
}

客户端

// Hub connection
//
var messageConnection = $.hubConnection(url);
var messageHub = messageConnection.createHubProxy('MessageHub');
$.connection.hub.logging = true

这篇关于在长期运行的过程中显示进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆