我想在我的Web应用程序中执行某些功能,而不像在Windows应用程序中的后台进程那样暗示用户正在运行的进程。
我希望在用户点击时触发后台处理,并希望将一些数据发送到这些功能。
任何人都可以建议我如何进行这项工作吗?
答案 0 :(得分:3)
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"
答案 1 :(得分:0)
也许您可以关注这些链接并了解如何处理该要求。
http://www.dotnetfunda.com/articles/article613-background-processes-in-asp-net-web-applications.aspx
http://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/
http://www.codeproject.com/Questions/137330/Run-a-background-job-in-ASP-NET
答案 2 :(得分:0)
虽然它解决了一个更复杂的问题(如何在使用IoC时将对象生命周期限定为请求),本文中的AsyncRunner类是一个很好的起点。