C#
Workers
public class Program
{
public static void Main(string[] args)
{
//Your code goes here
Console.WriteLine("Hello, world!");
d = 0;
//The TaskFactory is used to produce new Tasks with some options controlling how that task works
TaskFactory f = new TaskFactory(TaskCreationOptions.LongRunning, TaskContinuationOptions.None);
//This starts new Task and passes a lambda expression as the parameter. This lambda expression defines what the Task will do.
//In this particular case it will just call the DoWork() method.
var myTask = f.StartNew(() => DoWork());
//In case of a Thread I called Console.ReadLine() here to halt the execution of the main program's Thread so that my Thread could do some work.
//In case of a Task, I just wait for it to finish by calling the Wait() on the Task.
myTask.Wait();
}
private static int d;
public static void DoWork()
{
while(true)
{
//In case of a Thread to wait I had to call Thread.Sleep. In case of a Task I just run a predefined task responsible fo creating a delay and wait for it to finish.
Task.Delay(250).Wait();
//as you can see the 'd' field is a static member of the Program class and can be accessed from the DoWork because it's also a member of that clall. They both are in the same scope.
Console.WriteLine(d++);
if(d > 10) break;
}
}
}