Perform the action in a separate thread to unlock the interface

I have a form that is used to create a report. We use reports RDLC, and the report is loaded on the page aspx.

So, this is the code for Form, for the purpose of the form, a value is set _blankand opens in a new tab.

@using (Html.BeginForm("AssetReports", "AssetReports", FormMethod.Post, new { target = "_blank" }))
{
    <div class="row mt-15">
        <div class="col-md-12 text-center">
            <input type="submit" class="btn btn-primary" value="Show Report" />
        </div>
    </div>
}

This is a controller action that redirects to the aspx report page where the report is processed and displayed.

[HttpPost]
public void AssetReports(AssetReportsDTO model, AssetReportParametersDTO reportParameters)
{
    SessionHandler.AssetReport = model;
    SessionHandler.AssetReportParameters = reportParameters;

    switch (model.SelectedReportType)
    {
        case AssetReportTypesEnum.ExcessiveIdleReport:
            Response.Redirect("~/Reports/AssetReports/ExcessiveIdleReport/ExcessiveIdleReport.aspx");
            break;
    }
}

Reports take 3.4 minutes to generate in some cases. and during this time the user interface is blocked,

We want the report to be generated in a separate stream so that the user can use the user interface when creating the report.

Is there a way in C # MVC to execute this action in a separate thread?

, NULL

Task.Factory.StartNew(() =>
{
    switch (model.SelectedReportType)
    {
        case AssetReportTypesEnum.ExcessiveIdleReport:
            Response.Redirect("~/Reports/AssetReports/ExcessiveIdleReport/ExcessiveIdleReport.aspx");
            break;
    }
});

:

new Thread(() =>
{
    switch (model.SelectedReportType)
    {
        case AssetReportTypesEnum.ExcessiveIdleReport:
            Response.Redirect("~/Reports/AssetReports/ExcessiveIdleReport/ExcessiveIdleReport.aspx");
            break;
    }
}).Start();

- , 3 4 ExcessiveIdleReport.aspx

public partial class ExcessiveIdleReport1 : Page
    {
        private IReportsProvider _reportsProvider;

        protected void Page_Load(object sender, EventArgs e)
        {
            _reportsProvider = new ReportsProvider();
            if (!IsPostBack)
            {
                try
                {
                    var reportDetails = SessionHandler.AssetReport;
                    var reportParams = SessionHandler.AssetReportParameters;



                    var sPath = Server.MapPath("../ExcessiveIdleReport/ExcessiveIdleReport.rdlc");
                    var dsExcessiveReport =
                        _reportsProvider.GetExcessiveIdleReport(reportDetails.CompanyId, reportDetails.AssetId, reportDetails.StartDate,
                                                                reportDetails.EndDate, reportParams.SelectedIdleTime * 60);

                    ExcessiveIdleReportViewer.ProcessingMode = ProcessingMode.Local;
                    ExcessiveIdleReportViewer.LocalReport.EnableHyperlinks = true;
                    ExcessiveIdleReportViewer.HyperlinkTarget = "_blank";
                    ExcessiveIdleReportViewer.LocalReport.DataSources.Add(new ReportDataSource("ExcessiveIdleReport", dsExcessiveReport.Tables[0]));
                    ExcessiveIdleReportViewer.LocalReport.DataSources.Add(new ReportDataSource("ReportHeaderDetails", dsExcessiveReport.Tables[1]));
                    ExcessiveIdleReportViewer.LocalReport.DataSources.Add(new ReportDataSource("ReportSummary", dsExcessiveReport.Tables[2]));
                    ExcessiveIdleReportViewer.LocalReport.ReportPath = sPath;

                    ExcessiveIdleReportViewer.LocalReport.EnableExternalImages = true;
                    ExcessiveIdleReportViewer.LocalReport.SetParameters(param);

                    ExcessiveIdleReportViewer.LocalReport.Refresh();

                }
                catch (Exception ex)
                {
                    ErrorDiv.InnerText = string.Format("An error occured while generating the ExcessiveIdleReport, Please contact Support with following Message: [{0}] - [{1}]", ex.Message, ex.StackTrace);
                    ReportContentDiv.Visible = false;
                    ErrorDiv.Visible = true;
                }
            }
        }
    }

Ajax.BeginForm

 @using (Ajax.BeginForm("AssetReports", "AssetReports", new AjaxOptions() { HttpMethod = "POST", OnSuccess = "OpenReport"}, new { target = "_blank" }))
            {

            <div class="row mt-15">
                <div class="col-md-12 text-center">
                    <input type="submit" class="btn btn-primary" value="Show Report" />
                </div>
            </div>
}

JS:

function OpenReport(response) {
    var popup = window.open("about:blank", "_blank"); // the about:blank is to please Chrome, and _blank to please Firefox
    popup.location = '/TBReports/AssetReports/ExcessiveIdleReport/ExcessiveIdleReport.aspx';
}

Ajax:

Asset Reports 'Show Report', :

. View Group Reports , .

enter image description here

+4
3

ASP.NET MVC , , .

Microsoft https://msdn.microsoft.com/en-us/library/ms178581.aspx :

ASP.NET , , , . , ( SessionID ), . . ( , , -.) EnableSessionState @ ​​ ReadOnly, . , .

, , node.js. ( ...)

: http://johnculviner.com/asp-net-concurrent-ajax-requests-and-session-state-blocking/, :

MVC 3

, Microsoft ENUM System.Web.SessionState.SessionStateBehavior, . :

ReadOnly - , . . - , StateServer SQL, . , - . , .

MVC 3 Enum :

[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class TestController : Controller
{
    public ActionResult Index()
    {
        System.Threading.Thread.Sleep(10000);
        return new EmptyResult();
    }
}
+2

- -, . ASP.NET MVC . , .

AJAX. AJAX - , .

@using (Html.BeginForm( @using (Ajax.BeginForm(, . $jQuery.ajax(), , .

jQuery.Ajax()

$(document).delegate('#myForm', 'submit', function(e) {

    e.preventDefault();

    $.ajax({
        type: 'POST',
        dataType: 'json',
        url: '/AssetReports',
        data: $('#myForm').serialize(), // Post data from form
        success: function (responseData) {

             // Perform redirect to report?
        },
        error: function (jqXHR, textStatus, errorThrown) {

             // Display error?
        }
    })
});

@using (Html.BeginForm("AssetReports", "AssetReports", FormMethod.Post, new { target = "_blank", id = "myForm" })) // Added id
{
    <div class="row mt-15">
        <div class="col-md-12 text-center">
            <input type="submit" class="btn btn-primary" value="Show Report" />
        </div>
    </div>
}

Ajax.BeginForm

 function OpenReport(data, status, xhr) {

     // Open the report in a new window
     window.open("\\link\to\report");
 }

 @using (Ajax.BeginForm("AssetReports", "AssetReports", null, new AjaxOptions { HttpMethod = "POST", OnSuccess = "OpenReport" }, new { target = "_blank" }))
 {
     // ... Form
 }
+1

, , @goofballLogic, .

.aspx , readonly aspx Controller.

You can do this by adding an attribute EnableSessionState="ReadOnly"to the tagPage

<%@ Page Language="C#" EnableSessionState="ReadOnly" AutoEventWireup="true" CodeBehind="ExcessiveIdleReport.aspx.cs"" %>

It really does not matter whether you are using for this Ajax.BeginFormor Html.BeginForm.

0
source

Source: https://habr.com/ru/post/1612714/


All Articles