简体   繁体   English

结合使用MVC3和Async任务来更新UI

[英]Using MVC3 with Async tasks to update the UI

What I have is an AJAX form on a View that makes a call to the server. 我所拥有的是在View上调用服务器的AJAX表单。 This call perform n number of tasks where n is a number decided by records in a database (typically no more than 10 records). 此调用执行n个任务,其中n是由数据库中的记录决定的数量(通常不超过10个记录)。 Each record corresponds to a Build Definition in TFS so what I am trying to do is get all of these Build Definitions, queue them in TFS, and as each build completes update the UI so that user knows which builds have completed. 每个记录对应于TFS中的一个构建定义,因此我要做的就是获取所有这些构建定义,在TFS中将它们排队,并在每个构建完成后更新UI,以便用户知道哪些构建已完成。

Unfortunately I am not sure about how best to do this. 不幸的是,我不确定如何做到最好。 I was thinking something along these lines: 我在考虑以下思路:

        foreach (var dep in builds)
        {
            TFS tfsServer = new TFS(TFS_SERVER_ADDRESS);
            IBuildServer buildServer;
            int id = tfsServer.QueuBuild(dep.TeamProject, dep.BuildDefinition);
            string teamProject = dep.TeamProject;
            Task.Factory.StartNew(() => GetBuildStatus(teamProject, id, tfsServer));

        }

The task that is called is: 调用的任务是:

        private void GetBuildStatus(string TeamProject, int BuildID, TFS Server)
        {
            Server.GetBuildStatus(TeamProject, BuildID);
            AsyncManager.OutstandingOperations.Decrement();
        }

The problem here is that my Completed method isn't going to get called until all of the builds have completed. 这里的问题是,在所有构建完成之前,不会调用我的Completed方法。 How would I go about feeding data back up to the UI a piece at a time? 我将如何一次将数据反馈回用户界面?

It is also worth mentioning that the GetBuildStatus method looks like this: 还值得一提的是,GetBuildStatus方法如下所示:

        do
        {
            var build = buildsView.QueuedBuilds.FirstOrDefault(x => x.Id == BuildID);
            if(build != null)
            {
                status = build.Status;
                detail = build.Build;    
            }

        } while (status != QueueStatus.Completed);
        return detail.Status.ToString();

Given that the duration of a build will be longer than the timeout for an HTTP request you cannot leave the browser waiting while this happens. 鉴于构建的持续时间将长于HTTP请求的超时时间,因此您不能在这种情况发生时让浏览器等待。 You need to return a page and then poll for updates from that page using AJAX. 您需要返回一个页面,然后使用AJAX从该页面中轮询更新。 Typically you'd have a timer in javascript that triggers a regular call back to the server to get the updated status information. 通常,您会在javascript中使用一个计时器,该计时器触发对服务器的定期调用以获取更新的状态信息。

But, since you are using .NET you could also consider trying SignalR which lets you use long polling, server sent events or web sockets to wait for updates from the server and it wraps it all up in some easy to implement .NET classes and Javascript. 但是,由于您使用的是.NET,因此您也可以考虑使用SignalR ,它允许您使用长时间轮询,服务器发送的事件或Web套接字来等待服务器的更新,并将其全部包装在一些易于实现的.NET类和Javascript中。

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

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