立即重定向并继续后台处理

时间:2012-02-29 09:44:19

标签: asp.net

我有1.aspx页面进行长时间处理,并且我不会在此页面的任何位置显示计算结果。我想立即将浏览器重定向到2.aspx,但继续执行1.aspx中的进程。有可能吗?

1 个答案:

答案 0 :(得分:0)

你可以创建一个线程让它运行......这里有一个传递参数的选项的例子。

public class RunThreadProcess
{
    // Some parametres
    public int cProductID;

    // my thread
    private Thread t = null;

    // start it
    public Thread Start()
    {
        t = new Thread(new ThreadStart(this.work));
        t.IsBackground = true;
        t.SetApartmentState(ApartmentState.MTA);
        t.Start();

        return t;
    }

    // actually work
    private void work()
    {
        // do thread work
        all parametres are available here
    }
}

然后将其作为

运行
  var OneAction = new RunThreadProcess();
    OneAction.cProductID = 100;
    OneAction.Start();

只有一个功能的其他方式

  protected void Page_Load(object sender, EventArgs e)
    {
        ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
        ThreadPool.QueueUserWorkItem(state => Dokimes_Programming_multithread_QueryWorkThead.ThreadProc2());

        Debug.Write("Page Load ends.");
    }


    // This thread procedure performs the task.
    static void ThreadProc(Object stateInfo)
    {

        // No state object was passed to QueueUserWorkItem, so  stateInfo is null.
        Debug.Write(" Hello from the thread pool 1.");
    }

    static void ThreadProc2()
    {
        // No state object was passed to QueueUserWorkItem, so  stateInfo is null.
        Debug.Write("Hello from the thread pool 2.");
    }
相关问题