|
I am a newbie so excuse my rather basic question. I need to introduce scheduling to my current application in both WinForms and ASP.NET. How do I use your class to trigger a certain function or subroutine in my code like sending an SMTP mail. Here is my code:
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("your_email_address@gmail.com");
mail.To.Add("to_address");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("mail Send");
Thanks
|
|
|
|
|
Firstly, thanks for your efforts on this project - I think it will prove very useful.
This is only a small item, but line 58 of TaskScheduler.cs the enum name is set to MonthOfTheYeay instead of MonthOfTheYear.
In addition the HitTimeSpan property is never used - are there plans for this property or is it surplus to requirements?
Chris Bray
modified 21-Apr-15 5:49am.
|
|
|
|
|
thank you - I'll check this.
The HitTimeSpan is being planned to set a timespan in which a trigger is released.
With this you can increase the interval between two hit-tests.
|
|
|
|
|
Instead of firing a timer every 500msec you can use an autoresetevent or manualresetevent, and calculate the number of milliseconds till your next event and go sleep.
You should recalculate on each change in the schedule.
The same applies for your service thread, instead of peaking every 100 msec to shutdown you can also use an event here.
Good luck!
|
|
|
|
|
You are right, this is not really best practise...
But... there's a but!
What you suggest is exactly what we did in early versions of task scheduler
But over time we had trouble with daylight saving time or someone change system time.
UTC only solve the dlst problem.
|
|
|
|
|
That is correct, when the system date or time is changed the next event could occur sooner or later.
To go more in depth, if a task runs daily at 0:00:00 and at 0:00:05 the time is changed by some timesync back to 23:59:58, should you run the same task again or after 24 hours (hey, we found two files on our ftp... )?
|
|
|
|
|
hmm... bad thing...
that was not a problem until now for us...
But if yes the only thing you can do is to calculate a list of all triggers beforehand and set a flag for every event occured.
|
|
|
|
|
One thing "missing" from the Windows Task Scheduler is the ability to set task times in UTC - it only seems to use the current system time. Does yours, or could you make it, allow for this? And/or, have a flag that says "Ignore daylight saving time" when setting times - that would be teh next best thing.
|
|
|
|
|
Should not be that hard to implement UTC time values. I let you know when it is done.
|
|
|
|
|
Thanks - and, excellent project by the way!
|
|
|
|
|
Dear all, I am Suresh from SubhaVaastu dot com website. I am looking to have one task scheduler software for my website users. Can any body prepare for the payment. Those who are interested may quote for the same.
|
|
|
|
|
Your article was exactly what I am searching for, but I could not find a source code
|
|
|
|
|
Hi:
Before everything, I gotta appreciate you for this awesome article, I have been trying to find a TaskScheduler for days! But I already found it!
The thing is, I have this piece of code and it isn't ticking:
TaskScheduler taskScheduler = new TaskScheduler();
TaskScheduler.TriggerItem TestTask = new TaskScheduler.TriggerItem();
for (byte Day = 0; Day < 7; Day++)
{
switch (Day)
{
case 2:
TestTask.TriggerSettings.Weekly.DaysOfWeek[Day] = true;
break;
case 3:
TestTask.TriggerSettings.Weekly.DaysOfWeek[Day] = true;
break;
case 4:
TestTask.TriggerSettings.Weekly.DaysOfWeek[Day] = true;
break;
case 5:
TestTask.TriggerSettings.Weekly.DaysOfWeek[Day] = true;
break;
case 6:
TestTask.TriggerSettings.Weekly.DaysOfWeek[Day] = true;
break;
case 7:
TestTask.TriggerSettings.Weekly.DaysOfWeek[Day] = false;
break;
case 1:
TestTask.TriggerSettings.Weekly.DaysOfWeek[Day] = false;
break;
}
}
TestTask.StartDate = DateTime.MinValue;
TestTask.EndDate = DateTime.MaxValue;
TestTask.TriggerTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 15, 00, 00);
TestTask.OnTrigger += new TaskScheduler.TriggerItem.OnTriggerEventHandler(DoMagics);
TestTask.Enabled = true;
taskScheduler.AddTrigger(TestTask);
taskScheduler.Enabled = true;
}
private static void DoMagics(object sender, EventArgs args)
{
Console.WriteLine("Triggered at: " + DateTime.Now.ToString());
}
I set the DaysOfWeek to Tick on to the Laboral ones (Monday, Tuesday, Wednesday, Thursday, Friday) but it isn't ticking on 15:00.
Regards.
|
|
|
|
|
Really nice article sir!!
can you tell me after generating XML file,
How can i trigger in services according to that XML file,
I mean i use your code to generate XML file, and then now i am trying to create window services using c# which can read this XML file and fired trigger according to it.
I tried this
protected override void OnStart(string[] args)
{
TraceService("start service");
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 60000;
timer.Enabled = true;
}
protected override void OnStop()
{
timer.Enabled = false;
TraceService("stopping service");
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
TraceService(" Varun Another entry at " + DateTime.Now);
}
private void TraceService(string content)
{
FileStream fs = new FileStream(@"d:\Varun-Pc start up.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(content);
sw.Flush();
sw.Close();
connect();
string xmldata = "";
SqlDataAdapter da = new SqlDataAdapter("select * from Schedualer_Master", cn);
DataSet mds = new DataSet();
da.Fill(mds);
for (int i = 0; i < mds.Tables[0].Rows.Count; i++)
{
xmldata = "";
xmldata = mds.Tables[0].Rows[i]["XML_Data"].ToString();
TaskScheduler.TriggerItem newItem = TaskScheduler.TriggerItem.FromXML(xmldata);
newItem.Enabled = true;
_taskScheduler.AddTrigger(newItem);
_taskScheduler.Enabled = true;
}
object sender = new object();
EventArgs e = new EventArgs();
_taskScheduler._triggerTimer_Tick(sender, e);
}
modified 23-Nov-13 4:28am.
|
|
|
|
|
hello,
there is a new demo.
If you are still interested in implementing a windows service with task scheduler this could be interesting for you.
|
|
|
|
|
thank you very much sir for your reply.
i already created one Winservices which reading XML file every min from database. and fire trigger acordingly.
XML file which i created using your code.
Still i like to see new demo. where i can find it ?
|
|
|
|
|
help me!How to create a task in Windows Xp,using Microsoft.Win32.TaskScheduler.dll.
I tried, work in win7 no problem.
code:
private static void CreateTask()
{
var ts = new TaskService();
var td = ts.NewTask();
td.RegistrationInfo.Description = "Alert Message To User Task";
td.RegistrationInfo.Author = "Deepres";
td.RegistrationInfo.URI = "http://www.google.com/";
Trigger dt = new DailyTrigger { DaysInterval = 1 };
dt.ExecutionTimeLimit = TimeSpan.FromHours(1);
dt.StartBoundary = DateTime.Now.AddMinutes(3);
dt.Repetition.Duration = TimeSpan.FromHours(24);
dt.Repetition.Interval = TimeSpan.FromMinutes(30);
td.Triggers.Add(dt);
var sPath = Common.StrDecode(Cah.ReadValue("NavAppUrl", "NavAUrl"));
var exe = new ExecAction(sPath);
td.Actions.Add(exe);
ts.RootFolder.RegisterTaskDefinition(@"AlertMsgToUser", td);
}
|
|
|
|
|
The code doesn't trigger events if run as Windows Service. The reason for that is I think System.Windows.Forms.Timer. In this case it should be System.Timers.Timer.
|
|
|
|
|
ya, are right.
can you tell me how can i use this code for windows services,
i am using this code for generating XML file, and then i am trying to create windows services which can trigger according to XML data,
i tried this but it did not work!!
protected override void OnStart(string[] args)
{
TraceService("start service");
timer.Elapsed += new ElapsedEventHandler(OnElapsedTime);
timer.Interval = 60000;
timer.Enabled = true;
}
protected override void OnStop()
{
timer.Enabled = false;
TraceService("stopping service");
}
private void OnElapsedTime(object source, ElapsedEventArgs e)
{
TraceService(" Varun Another entry at " + DateTime.Now);
}
private void TraceService(string content)
{
FileStream fs = new FileStream(@"d:\Varun-Pc start up.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.WriteLine(content);
sw.Flush();
sw.Close();
connect();
string xmldata = "";
SqlDataAdapter da = new SqlDataAdapter("select * from Schedualer_Master", cn);
DataSet mds = new DataSet();
da.Fill(mds);
for (int i = 0; i < mds.Tables[0].Rows.Count; i++)
{
xmldata = "";
xmldata = mds.Tables[0].Rows[i]["XML_Data"].ToString();
TaskScheduler.TriggerItem newItem = TaskScheduler.TriggerItem.FromXML(xmldata);
newItem.Enabled = true;
_taskScheduler.AddTrigger(newItem);
_taskScheduler.Enabled = true;
}
object sender = new object();
EventArgs e = new EventArgs();
_taskScheduler._triggerTimer_Tick(sender, e);
}
|
|
|
|
|
Nice job, the logic is in one class this is very useful!
Thanks
|
|
|
|
|
if on time run task system off after system do on are run task ???
if The time is past after sysetem on are do run task .??
|
|
|
|
|
Sorry, I do not understand your question
|
|
|
|
|
I think s/he means will a task be run asap after a restart if the system is off at its last scheduled run time?
|
|
|
|
|
Natively this is not possible because this class don't save raised triggers to a file or a database.
But you can perform Runcheck(DateTime) for every day since last program shutdown until now.
|
|
|
|
|
I little bit modify your taskscheduler.cs to run my batch file
void _triggerTimer_Tick(object sender, EventArgs e)
{
_triggerTimer.Stop();
foreach (TriggerItem item in TriggerItems)
if (item.Enabled)
while (item.TriggerTime <= DateTime.Now)
{
item.RunCheck(DateTime.Now);
System.Diagnostics.Process.Start("C:\\Users\\ST_User_1\\Desktop\\DGFTServer");
}
_triggerTimer.Start();
}
its run, properly, but when I terminate program this scheduling is not running,
so, is there any way to run my batch file even if i terminate program like window task scheduler
|
|
|
|