【问题标题】:Cancel a method by user input or non response通过用户输入或无响应取消方法
【发布时间】:2015-06-22 22:29:11
【问题描述】:

我正在开发的网站允许用户在用户选择的两个日期之间生成报告。我遇到的问题是,如果用户意外地在巨大的日期差异之间请求报告,即一年,服务器会承受很大的压力,有时可能会导致所有用户崩溃。我要做的是有某种进度条向用户显示报告将花费多长时间,以及一个取消按钮或一个按钮来检查它们是否仍然存在。我正在考虑在任务中使用取消令牌,但不确定这是否是我想要的。还在考虑一个全局链表,其中随机数链接到真假,因为用户可以同时打开许多报告。我在下面包含了一些相关代码。我将不胜感激任何帮助或正确方向的观点,谢谢

//java脚本,它是从日期传递过来的,调用方法genonereport

window.open('genonereport?idd=' + idd + '&fromm=' + 
fromm + '&too=' + too + '&filetype=' + outputValue ,
 '_blank'); 

// genonereport 将值传递给下面包含相关方法的类 ---report.render 这是如果用户取消报告或他们离开时我要取消的方法

     public byte[] genReportBytes(int id, string fromm, string too, string filetype)
    {

        reportDetails repD = new reportDetails();
        repD = getOneReport(id);

        LocalReport report = new LocalReport();

        if (fromm != null)
            repD.ParametersCommandLine = "@startdate=" + fromm;

        if (too != null)
            repD.ParametersCommandLine += " @enddate=" + too;

        string RDLCPath = ConfigurationManager.AppSettings["RDLCPath"];
        string ReportOutputPath = ConfigurationManager.AppSettings["ReportOutputPath"];

        string RDLCName = repD.RDLCName;
        RDLCPath += @"\" + RDLCName;
        report.ReportPath = RDLCPath;

        string sqlGet = repD.SQLOfReport;

        report.DataSources.Add(new ReportDataSource(repD.DatasetName, getReportData(sqlGet, repD.ParametersCommandLine)));

        // export to byte array

        Warning[] warnings;
        string[] streamids;
        string mimeType;
        string encoding;
        string filenameExtension;
        string deviceInf = "";
        byte[] bytes = new byte[64];
        byte[] empty = null; 
        string extension;
        bool Completed;

        if (filetype == "pdf")
        {
            deviceInf = "<DeviceInfo><PageHeight>8.5in</PageHeight><PageWidth>11in</PageWidth><MarginLeft>0in</MarginLeft><MarginRight>0in</MarginRight></DeviceInfo>";
            //fileName = ReportOutputPath + @"\" + repD.NameOfOutputPDF + ".PDF";
            //Completed = ExecuteWithTimeLimit(TimeSpan.FromMilliseconds(5000), () =>
            //{
                bytes = report.Render("pdf", deviceInf, out mimeType, out encoding, out filenameExtension,
                                        out streamids, out warnings);
            //});

        }
        else
        {
            //fileName = ReportOutputPath + @"\" + repD.NameOfOutputPDF + ".XLS";
            //Completed = ExecuteWithTimeLimit(TimeSpan.FromMilliseconds(5000), () =>
            //{
            bytes = report.Render(
                  "Excel");
            //});
        }
        //if (Completed == true)
        //{
            return bytes;
        //}
        //else
            //return empty;
    }

// 我已经做到了这一点,但这只会在一定时间后取消,这对我来说并不是什么好事

public static bool ExecuteWithTimeLimit(TimeSpan timeSpan, Action codeBlock)
    {
        try
        {
            Task task = Task.Factory.StartNew(() => codeBlock());
            task.Wait(timeSpan);
            return task.IsCompleted;
        }
        catch (AggregateException ae)
        {
            throw ae.InnerExceptions[0];
        }
    }

抱歉这个冗长的问题,再次感谢

【问题讨论】:

  • 作为第一步,为什么不将用户可以选择的时间跨度限制为 6 个月?
  • 遗憾的是,有时用户确实需要跨越一年的报告,因为有些报告是汇总报告,仅汇总每天的数据,并且不需要很长时间即可呈现。但是,如果用户运行一年的每日报告,其中包含每个给定日期的十分钟间隔值,并且图表问题开始出现
  • 因此,如果您有办法辨别他们想要运行哪种类型的报告、摘要与否,您可以限制日期......例如使用水晶报告,您只需在选择报告后输入参数...也许您可以做类似的事情...选择摘要...让用户指定日期...选择每日...让用户指定有限制的日期。
  • “报告需要多长时间”你怎么可能估计这个?考虑网络争用、数据库可用性、cpu 工作负载等时

标签: c# asp.net


【解决方案1】:

您将需要至少三个 Web 端点:

  1. 一个生成报告并返回结果
  2. 您的 Javascript 可以定期请求以检查进度
  3. 您的浏览器可以请求取消任务的任务

您需要将每个任务的句柄存储在带有某种 id 的字典中,这样您就可以将 id 传递给查询 2 和 3。

This article 为您提供了如何执行此操作的示例。

这并不简单,因此限制用户可以生成的报告的大小可能是首选选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-08
    相关资源
    最近更新 更多