ASP.NET Schedule deletion of temporary files

前端 未结 8 1012
暗喜
暗喜 2021-01-06 00:38

Question: I have an ASP.NET application which creates temporary PDF files (for the user to download). Now, many users over many days can create many PDFs, which take much di

8条回答
  •  北海茫月
    2021-01-06 01:10

    The Best way is to create a batch file which it be called by the windows task scheduler one at the interval that you want.

    OR

    you can create a windows service with the class above

    public class CleanUpBot
    {
    
    public bool KeepAlive;
    
    private Thread _cleanUpThread;
    
    public void Run()
    {
    
    _cleanUpThread = new Thread(StartCleanUp);
    
    }
    
    private void StartCleanUp()
    {
    
    do
    
    {
    
    // HERE THE LOGIC FOR DELETE FILES
    
    _cleanUpThread.Join(TIME_IN_MILLISECOND);
    
    }while(KeepAlive)
    
    }
    
    }
    

    Notice that you can also call this class at the pageLoad and it wont affect the process time because the treatment is in another thread. Just remove the do-while and the Thread.Join().

提交回复
热议问题