Skip to main content

Posts

Showing posts with the label Windows Task

Taks and Thread.Sleep

What is the problem of the following code? Task.Factory.StartNew(() => { while (true) { Console.WriteLine("@ " + DateTime.Now.ToString()); Thread.Sleep(TimeSpan.FromSeconds(2)); } }); var task = Task.Factory.StartNew(() => { while (true) { Console.WriteLine("! " + DateTime.Now.ToString()); Thread.Sleep(TimeSpan.FromSeconds(4)); } }); Task.WaitAll(task); The main problem is related to Thread.Sleep. Using this method in combination with tasks we can gen odd behavior and in the end the performance of the tasks can be affected. One of the behavior that we could get is related to how the sleep period – that will not be the same as we expect ("random" sleep interval). This occurs because of the TPL behavior, which will not guarantee that each task will have a dedicated thread where he can run. What we should do to resolve this problem? We have two possible solutio...

CRUD operation on Windows Task from .NET

Zilele trecute am gasit o librarie simpla si foarte usor de folosit pentru a crea task-uri sub Windows. Aceasta poarta numele de Task Scheduler Managed Wrappe si o puteti gasi la urmatoarea adresa: http://taskscheduler.codeplex.com/ Primul lucru care mi-a atras atentia a fost cat de usor se poate integra intr-o aplicatia deja existenta. Clasa de baza, in jurul careia se invarte totul este TaskService, prin intermediul caruia putem sa inregistram task-uri noi sau sa le manipulam pe cele deja existente. Fiecare task se identifica unic printr-un nume. Pe baza acestui nume putem sa obtinem un task deja existent. La fiecare task putem sa inregistram una sau mai multe actiuni care sa se execute. Actiunile pot sa fie de orice fel, incepand de la trimiterea unui email sau rularea unui executabil pana la afisarea unui mesaj la utilizator. Acelasi lucru se intampla si cu trigerurile, toate cele existente pe Windows exista si in aceasta librarie: EventTrigger DailyTrigger WeeklyTrigger MonthlyTri...