如果网页关闭,则完成QueueBackgroundWorkItem [英] Getting QueueBackgroundWorkItem to complete if the web page is closed

查看:439
本文介绍了如果网页关闭,则完成QueueBackgroundWorkItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据文献我读过有关QueueBackgroundWorkItem的文章,我应该能够在后台完成一项长任务,而不会被IIS打断。

According to literature I've read about QueueBackgroundWorkItem, I should be able to complete a long task in the background without being interrupted by IIS.

我是什么想知道是否有可能,如果我开始一个长任务并在它完成之前关闭我的网站,QueueBackgroundWorkItem不应该完成任务吗? (目前不是)

What I'm wondering if is possible, if I start a long task and close my website before it completes, shouldn't the QueueBackgroundWorkItem finish the task? (it's not currently)

这是我的电话:

private async Task WriteTextAsync(CancellationToken cancellationToken)
{
    string filePath = printPath;
    string text;
    byte[] encodedText = Encoding.Unicode.GetBytes(text);

    for (int i = 0; i < 200; i++)
    {
        text = "Line " + i + "\r\n";
        encodedText = Encoding.Unicode.GetBytes(text);
        using (FileStream sourceStream = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.None,                    bufferSize: 4096, useAsync: true))
        {
            await sourceStream.WriteAsync(encodedText, 0, encodedText.Length);
        };
        Thread.Sleep(200);
    }
}

private void QueueWorkItem()
{
    Func<CancellationToken, Task> workItem = WriteTextAsync;
    HostingEnvironment.QueueBackgroundWorkItem(workItem);
}

编辑:我已经让这个工作了。我把它整理了一下。这现在在浏览器关闭后执行,大约3-4分钟,所有内容都写入文件。感谢所有参与其中的人。

I have gotten this to work. I trimmed it up. This now executes after the browser is closed, about 3-4 minutes, everything is written to file. Thanks for all who chipped in.

private void QueueWorkItem()
{
    HostingEnvironment.QueueBackgroundWorkItem(cancellationToken =>
    {
        string filePath = printPath;
        string text = "File line ";
        TextWriter tw = new StreamWriter(printPath);

        for (int i = 0; i < 400; i++)
        {
            text = "Line " + i;

            tw.WriteLine(text);

            Thread.Sleep(200);
        }

        tw.Close();
    });
}


推荐答案

当你使用 HostingEnvironment.QueueBackgroundWorkItem ,你必须记住,任务是在asp.net工作进程中运行的。因此,如果IIS在没有请求20分钟后关闭工作进程(这是默认设置),那么您的任务也将关闭。

When you use HostingEnvironment.QueueBackgroundWorkItem, you have to keep in mind that the task is running in asp.net worker process. So if IIS shuts the worker process down after 20 minutes of no requests (this is the default), then your task will go down as well.

如果您的后台任务应该每隔X次运行一次,然后将它添加到global.asax Application_Start是个好主意。这样的事情:

If your background task should run every X time, then it would be a good idea to add it to global.asax Application_Start. Something like this:

在global.asax中Application_Start:

In global.asax Application_Start:

System.Web.Hosting.HostingEnvironment.QueueBackgroundWorkItem(async (t) => { await BackgroundJob.RunAsync(t); });

您的背景课程:

public static class BackgroundJob
{
   public static async Task RunAsync(CancellationToken token)
   {
      ... your code

      await Task.Delay(TimeSpan.FromMinutes(5), token);
   }
}

IIS应用程序池配置

在应用程序池高级设置中,将空闲超时更改为零,这样当您没有请求一段时间后,后台任务就不会停止运行:

In app pool advanced settings, change Idle timeout to zero, so your background task will not stop running when you have no request for a period of time:

这篇关于如果网页关闭,则完成QueueBackgroundWorkItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆