Solved

Windows service with 24 hour trigger

Posted on 2007-03-23
5
345 Views
Last Modified: 2013-11-26
I am creating a service that will create a PDF with a date included in the name of the file. The PDF is created from other PDFs that accumulate within a group of folders. Every morning at a user defined time (probably 7AM) all the files that have been accumlated will be cleared out and new files will start to accumlate and a new PDF will be created with a new date included file name. I will have a timer that creates the new PDF every few minutes.

My question is how do I trigger the clearing out the PDFs in the accumlation folders at the user defined time? I can imagine a couple of methods that might work but hoped that someone might suggest a proven method.
0
Comment
Question by:bbaldwin
5 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 18782368
Here is the basic idea using a polling timer:

Public Class Form1

    Private targetTime As DateTime
    Private WithEvents tmr As New System.Windows.Forms.Timer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        targetTime = New DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day, 7, 0, 0) ' 7:00 am
        If DateTime.Now.Subtract(targetTime).TotalSeconds >= 0 Then
            ' past target time already...schedule it for tomorrow
            targetTime = targetTime.AddDays(1)
        End If

        tmr.Interval = 1000 ' increase this if you like
        tmr.Start()
    End Sub

    Private Sub tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmr.Tick
        If DateTime.Now.Subtract(targetTime).TotalSeconds >= 0 Then
            targetTime = targetTime.AddDays(1) ' schedule it again for tomorrow

            ' do something in here...

        End If
    End Sub

End Class
0
 
LVL 2

Author Comment

by:bbaldwin
ID: 18782811
That looks like it would work real good!! Let me test it.
0
 
LVL 41

Expert Comment

by:graye
ID: 18783051
I'd recommend that you just create a console application and use the built-in Scheduled Tasks to determine when the program runs.
0
 
LVL 8

Expert Comment

by:Autoeforms
ID: 18784823
you could start a seperate time in the service and add your test there to the next date of the run.

as a service you will have to store the users start time in a configuration file if you have not figured this  at already.
 
make your interval in minutes (1440) minutes is 1 day. this way if it needs to change in the future to occurr twice a day or evey hour your code is easily changed.  and if you pass the parameter in with start time you don't even have to change the code.

finally why delete yesterdays data immediatly. Not sure what  the data is but my users generally ask to see the old data at some point for comparison. could you not create a new directory each time the  directory name as a date and time then write to that. You would then delete directories that are say 7 days old.

You might not need all this functionality but sometimes it helps to protect yourself in the initial design

greg
0
 
LVL 2

Author Comment

by:bbaldwin
ID: 18785129
Graye,

the service is really doing much more than I indicated (checking for files in emails and folders, converting files to PDFs from other sources, etc.) This needs to be done quite often so a scheduled task will not work for us. Thanks for the suggestion.

AutoeForm,

already added the item to the configuration file, thanks. The files that we are going to delete are actually copies of files from other folders and this is just the staging folders for the PDFs for this day and therefore can be deleted whenever. Thanks for the great suggestions though.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now