How to start a background process in an ASP.Net web application

I want to perform some function in my web application without telling the user about the current process, like a background process in a Windows application.

I want to call a background process when the user clicks and wants to send some data to this function.

Can someone suggest me how this can be done?

+2
source share
3 answers
private readonly BackgroundWorker backgroundWorker1 = new BackgroundWorker();

protected void Page_Load(object sender, EventArgs e)
    {
        this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
    }

    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            timing();
        }
        catch
        {
        }
    }

    public void timing()
    {
        string tt = DateTime.Now.ToString("tt");
        string t = "";
        if (tt == "AM")
        {
            int hour = DateTime.Now.Hour;
            if ((hour >= 0) & (hour <= 11))
            {
                t = "GOOD MORNING";
            }
        }
        else if (tt == "PM")
        {
            int hour = DateTime.Now.Hour;
            if ((hour >= 12) & (hour <= 15))
            {
                t = "GOOD AFTERNOON";
            }
            else if ((hour >= 16) & (hour <= 23))
            {
                t = "GOOD EVENING";
            }
        }
        Label1.Text = t;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        backgroundWorker1.RunWorkerAsync();
    }


//SOURCE:

<%@ Page Language="C#" Async="true" AutoEventWireup="true" CodeFile="background.aspx.cs" Inherits="background" %> //Async="true"
+2
source

Although it solves a more complex problem (how to do this using IoC, where the lifetime of objects is limited by requests), the AsyncRunner class from this article is a good starting point.

Run background tasks from MVC actions using Autofac

0
source

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


All Articles