Friday, February 02, 2018

creating TPL Tasks c#


Task.Factory.StartNew(() => CalDaysAndSendMail(taskList), TaskCreationOptions.LongRunning);


Task has constructor from Action or Action:

Action a = myVoidMethodWithoutParameters;
Task t = new Task(a);

kurz: var t = new Task(myVoidMethodWithoutParameters);

           Action a = DoSomething;
            Task t = new Task(a,obj);

void DoSomething(object o)

from https://www.codeproject.com/articles/189374/the-basics-of-task-parallelism-via-c:

        // use an Action delegate and named method
        Task task1 = new Task(new Action(printMessage));
        // use an anonymous delegate
        Task task2 = new Task(delegate { printMessage() });
        // use a lambda expression and a named method
        Task task3 = new Task(() => printMessage());
        // use a lambda expression and an anonymous method
        Task task4 = new Task(() => { printMessage() });:

    private static void printMessage() {
        Console.WriteLine("Hello, world!");
    }

No comments: