模拟Windows服务使用ASP.NET [英] Simulate Windows Service with ASP.NET

查看:160
本文介绍了模拟Windows服务使用ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我小的web应用程序,生成PDF文件作为报告。我想,他们是产生10秒钟后删除这些生成的PDF文件。我想要做的就是读取文件夹的PDF文件每隔10秒,并删除所有的PDF文件,该文件夹内。

I have small web app that generate PDF files as a report. I'm trying to delete those generated PDF files after 10 sec that they are generated. What I want to do is to read a folder with PDF files every 10 sec, and delete all the PDF files inside that folder.

我在读易ASP后台任务的这个职位。 NET 。下面code是VB版本。

I read this post of Easy Background Tasks in ASP.NET. The following code is the VB version.

    Protected Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    AddTask("DoStuff", 10)
End Sub

Private Sub AddTask(ByVal name As String, ByVal seconds As Integer)
    OnCacheRemove = New CacheItemRemovedCallback(CacheItemRemoved)
    HttpRuntime.Cache.Insert(name, seconds, Nothing, DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, _
     OnCacheRemove)
End Sub

Public Sub CacheItemRemoved(ByVal k As String, ByVal v As Object, ByVal r As CacheItemRemovedReason)
    ' do stuff here if it matches our taskname, like WebRequest

    DeletePDFilesInFoler()

    ' re-add our task so it recurs
AddTask(k, Convert.ToInt32(v))

结束小组

但我得到这个错误

代表
  System.Web.Caching.CacheItemRemovedCallback
  需要一个'AddressOf'前pression或
  拉姆达前pression作为唯一的参数
  其构造。

Delegate 'System.Web.Caching.CacheItemRemovedCallback' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor.

如果这个code工作,在那里我应该把它。对了,现在我把它的母版页。如何得到这个错误呢?

If this code works, where I should put it. Right, now I'm putting it in the master page. How to get this error out?

感谢您

推荐答案

而不是按计划删除它们,为什么不找旧的PDF文件,并​​生成一个PDF每次删除它们?

Rather than deleting them on a schedule, why not look for old PDFs and delete them each time a PDF is generated?

伪code:

/* Clean up old PDFs */
If Not StaticCleaningUpPDFsNow Then
   // No other reports generating simultaneously and trying to delete old PDFs
   StaticCleaningUpPDFsNow = True 
   For each f in (reportfolder\*.pdf)
      If f.DateCreated.AddSeconds(10) < Now Then f.Delete
   Next
   StaticCleaningUpPDFsNow = False
End If
/* Create PDF for the current report */
...

开销查找文件的文件夹中,并删除一些是令人难以置信的小,并在需要做的,这是一个更好的利用资源,周围无缓存到期的黑客(这可能会导致边缘的情况下)。

The overhead to look for files in a folder and delete a few is incredibly small, and doing this on demand is a much better use of resources, without the hacks around cache expirations (and the edge cases that can result).

这篇关于模拟Windows服务使用ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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