|
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
|
|
|
|
|
No, like you saied. The Program is terminated.
You can run it as a separate process or as a Windows Service (like the Windows Task Scheduler)
|
|
|
|
|
ya, but after generating XML file with help of your code,
Now i am trying to create window services which read XML file every one minute and trigger according to XML data.
I try a lot but still i am not able to done this,
Kindly please help me for this.
in this same article in other question i mention what i tried
modified 23-Nov-13 4:41am.
|
|
|
|
|
Regarding.
private bool TriggerOneTimeOnly(DateTime date)
{
return (_TriggerSettings.OneTimeOnly.Active && (_TriggerSettings.OneTimeOnly.Date == date));
}
I've heavily modifed the code for my own purposes(but tried to maintain the way it functions, but I believe I may have run across a bug.
FindNextTriggerDate(DateTime lastTriggerDate) calls CheckDate(date.Date) which calls TriggerOneTimeOnly(DateTime date)
I believe _TriggerSettings.OneTimeOnly.Date is actually a date with a time portion.
If I understand the code correctly, I think you meant to have _TriggerSettings.OneTimeOnly.Date.Date instead.
Am I correct in this that the code is in error?
|
|
|
|
|
Kinda a c# newbie here. The code you have written is excellent. I am currently writing my first application and would like to use your code. My question, forgive my ignorance: I have a task that runs say daily at 3:00 PM. Where in your code is the actual event fired? Where do I place my code that would get executed?
|
|
|
|
|
Nice. But I have a question why not use Quartz.Net? It does provide the features you mentioned under So why another Task Scheduler.
|
|
|
|
|
Great work! Very useful task scheduler. I have one problem. I have a similar problem with exporting/importing XML. When I export I get this error:
="1.0"="utf-16"
<TriggerItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Tag xsi:type="xsd:string">New Item</Tag>
<Enabled>true</Enabled>
<StartDate>2013-03-25T00:00:00-05:00</StartDate>
<EndDate>2014-03-25T00:00:00-05:00</EndDate>
<TriggerTime>2013-03-25T17:22:59</TriggerTime>
<TriggerSettings>
<OneTimeOnly>
<Date>2013-03-25T00:00:00-05:00</Date>
<Active>true</Active>
</OneTimeOnly>
<Daily>
<Interval>0</Interval>
</Daily>
<Weekly>
<DaysOfWeek>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
</DaysOfWeek>
</Weekly>
<Monthly>
<Month>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
</Month>
<DaysOfMonth>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
</DaysOfMonth>
<WeekDay>
<WeekNumber>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
</WeekNumber>
<DayOfWeek>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
</DayOfWeek>
</WeekDay>
</Monthly>
How do I go about automatically export/import XML upon open/close? Also, when I export, is a XML file created? If so, what is it called and does it appear in the bin folder?
Thank you
modified 26-Mar-13 1:00am.
|
|
|
|
|
Thanks Very Much For Your Priceless Article.
|
|
|
|
|
Excelent Work! but the static method FromXML() it doesn´t work for me . can anyone help me?
|
|
|
|
|
please give us detailed information (error message, your code...)
|
|
|
|
|
Thanks for the answer!!. The error is "No se controlo InvalidOperationException.Error en el documento XML(5,4)". The code is:
TaskScheduler.TaskScheduler.TriggerItem.FromXML(textBoxEvents.Text);
In the textBoxEvents.text is the full XML string
Can please give an example of how to use the method
Thank you very much!!!!
="1.0"="utf-16"
<TriggerItem xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Tag xsi:type="xsd:string">Enviar Clientes y Datos Maestros</Tag>
<Enabled>true</Enabled>
<StartDate>2012-11-14T00:00:00-06:00</StartDate>
<EndDate>2013-11-14T00:00:00-06:00</EndDate>
<TriggerTime>2012-11-14T08:30:19</TriggerTime>
<TriggerSettings>
<OneTimeOnly>
<Date>2012-11-14T00:00:00-06:00</Date>
<Active>true</Active>
</OneTimeOnly>
<Daily>
<Interval>0</Interval>
</Daily>
<Weekly>
<DaysOfWeek>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
</DaysOfWeek>
</Weekly>
<Monthly>
<Month>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
</Month>
<DaysOfMonth>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
</DaysOfMonth>
<WeekDay>
<WeekNumber>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
</WeekNumber>
<DayOfWeek>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
<boolean>false</boolean>
</DayOfWeek>
</WeekDay>
</Monthly>
</TriggerSettings>
</TriggerItem>
|
|
|
|
|
|
i had downloaded your code and run it .now i want it to trigger a service i created n c#. how can i use this program to trigger my window service app? i want to add my code that will trigger the win service?pls help
|
|
|
|