系统相关
首页 > 系统相关> > CodeGo.net>如何在ASP.Net Web应用程序中运行后台进程

CodeGo.net>如何在ASP.Net Web应用程序中运行后台进程

作者:互联网

我想在Web应用程序中执行某些功能,而又不像Windows应用程序中的后台进程那样使用户对正在运行的进程产生任何影响.

我想在用户单击时触发后台进程,并且也想向这些功能发送一些数据.

有人可以建议我如何执行此操作吗?

解决方法:

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"

标签:background-process,asp-net,c
来源: https://codeday.me/bug/20191123/2066467.html