得到适当的最新进展在ASP.NET两个早就在等待并发进程 [英] Get Proper Progress Updates on Two Long Waited Concurrent Processes in ASP.NET

查看:128
本文介绍了得到适当的最新进展在ASP.NET两个早就在等待并发进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用.NET的ffmpeg包装 http://www.mediasoftpro.com 用进度条来实现后台视频处理指示来计算多少视频处理和发送信息的网页更新进度条指示器。其工作罚款,如果只是单一的过程在工作一段时间,但两个并发进程的情况下,(启动两个视频发布一次让来自两个不同的计算机说的),进度条突然喜忧参半状态。
这里是我的code,其中我用静态对象正确发送单一实例的信息,以进度条。

I implemented background video processing using .net ffmpeg wrapper http://www.mediasoftpro.com with progress bar indication to calculate how much video is processed and send information to web page to update progress bar indicator. Its working fine if only single process works at a time, but in case of two concurrent processes (start two video publishing at once let say from two different computers), progress bar suddenly mixed progress status. Here is my code where i used static objects to properly send information of single instance to progress bar.

static string FileName = "grey_03";
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        if (Request.Params["file"] != null)
        {
            FileName = Request.Params["file"].ToString();
        }
    }
}
public static double ProgressValue = 0;
public static MediaHandler _mhandler = new MediaHandler();

[WebMethod]
public static string EncodeVideo()
{
    // MediaHandler _mhandler = new MediaHandler();
    string RootPath = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath);
    _mhandler.FFMPEGPath = HttpContext.Current.Server.MapPath("~\\ffmpeg_july_2012\\bin\\ffmpeg.exe");
    _mhandler.InputPath = RootPath + "\\contents\\original";
    _mhandler.OutputPath = RootPath + "\\contents\\mp4";
    _mhandler.BackgroundProcessing = true;
    _mhandler.FileName = "Grey.avi";
    _mhandler.OutputFileName =FileName;
    string presetpath = RootPath + "\\ffmpeg_july_2012\\presets\\libx264-ipod640.ffpreset";
    _mhandler.Parameters = " -b:a 192k -b:v 500k -fpre \"" + presetpath + "\"";
    _mhandler.OutputExtension = ".mp4";
    _mhandler.VCodec = "libx264";
    _mhandler.ACodec = "libvo_aacenc";
    _mhandler.Channel = 2;
    _mhandler.ProcessMedia();
    return _mhandler.vinfo.ErrorCode.ToString();
}

[WebMethod]
public static string GetProgressStatus()
{
    return Math.Round(_mhandler.vinfo.ProcessingCompleted, 2).ToString();
    // if vinfo.processingcomplete==100, then you can get complete information from vinfo object and store it in database and perform other processing.
}

下面是负责后每秒钟更新等进度条显示的jQuery功能。

Here is jquery functions responsible for updating progress bar indication after every second etc.

$(function () {
         $("#vprocess").on({
             click: function (e) {
                 ProcessEncoding();
                 var IntervalID = setInterval(function () {
                     GetProgressValue(IntervalID);
                 }, 1000);
                 return false;
             }
         }, '#btn_process');

     });
     function GetProgressValue(intervalid) {
         $.ajax({
             type: "POST",
             url: "concurrent_03.aspx/GetProgressStatus",
             data: "{}",
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: function (msg) {
                 // Do something interesting here.
                 $("#pstats").text(msg.d);
                 $("#pbar_int_01").attr('style', 'width: ' + msg.d + '%;');
                 if (msg.d == "100") {
                     $('#pbar01').removeClass("progress-danger");
                     $('#pbar01').addClass("progress-success");
                     if (intervalid != 0) {
                         clearInterval(intervalid);
                     }
                     FetchInfo();
                 }
             }
         });
     }

问题的出现是由于静态mediahandler对象

The problem arises due to static mediahandler object

public static MediaHandler _mhandler = new MediaHandler();

我需要一种方法来保持两个并发进程的信息彼此分开,以便更新进度条与值完全属于这一进程。

I need a way to keep two concurrent processes information separate from each other in order to update progress bar with value exactly belong to that process.

推荐答案

我觉得你有一个误区这里。

I think you have a misunderstanding here.

由于您使用的是静态变量,您所看到的并发问题。这是因为在ASP.NET中,静态变量是由所有的请求,因为他们曾经是每AppDomain中,直到应用程序池回收共享。

Because you are using a static variable, you are seeing concurrency problems. This is because in ASP.NET, static variables are shared by all requests because they are once-per-AppDomain until the app pool recycles.

您应该揭露一个WCF的终点,你可以用它是否完成或不问你的服务。该服务应该工作队列,并调用你的的ffmpeg 程序队列中的每个项目。您还没有提到有多少用户会调用你的页面,但它可能是更好的这种方式让您可以更简单地控制服务器多少项目允许一次处理设计它。因为你是下一个服务环境,而不是ASP.NET下运行的过程中,你也将运行到少的问题。

You should expose a WCF end-point that you can use to ask your service whether it is finished or not. This service should work a queue and call your ffmpeg program for each item in the queue. You haven't mentioned how many users will be calling your page, but it's probably better to design it this way so you can more simply control how many items the server is allowed to process at once. You will also run in to less problems because you are running a process under a service context rather than under ASP.NET.

这篇关于得到适当的最新进展在ASP.NET两个早就在等待并发进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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