none
Task - Task.Run - Task.Factory.StartNew RRS feed

  • Question

  • Hello,
    Can someone complete these 2 variants as t.
    - tVariant2
    - tVariant3
    I want to get to know the differences?
    Task.Run(()
    Is available as of .NET4.5. Which variant should we currently take?
    Greetings Markus
    static void Main(string[] args)
    {
    	CKontakte kontakt = new CKontakte { Nachname = "Tanja", Vorname = "Heinz", Telefon = "019034567" };
    
    	Console.BackgroundColor = ConsoleColor.White;
    	Console.ForegroundColor = ConsoleColor.Black;
    	Console.Clear();
    
    	Console.WriteLine("Start");
    	// Input for lambda
    	var t = new Task((myData) =>
    	{
    		// We have to type the transfer value:
    		CKontakte myLocalObject = (CKontakte)myData;
    		Thread.Sleep(1000);
    
    		//Use the data
    		Console.WriteLine(myLocalObject.Nachname);
    		Console.WriteLine(myLocalObject.Vorname);
    		Console.WriteLine(myLocalObject.Telefon);
    		Console.WriteLine("Stop Task");
    
    		// Here the transfer of data to the task takes place
    	}, kontakt, TaskCreationOptions.LongRunning);
    
    	Console.WriteLine("Start Task");
    	t.Start();
    
    	// With Run I need not the Start
    	var tVariant2 = Task.Run(() =>
    	{
    		//CKontakte myLocalObject = (CKontakte)kontakt;
    		//Thread.Sleep(1000);
    	   
    		//Console.WriteLine(myLocalObject.Nachname);
    		//Console.WriteLine(myLocalObject.Vorname);
    		//Console.WriteLine(myLocalObject.Telefon);
    		Console.WriteLine("Stop Task");
    	});
    	
    	var tVariant3 = Task.Factory.StartNew(() =>
    	{
    		//CKontakte myLocalObject = (CKontakte)kontakt;
    		//Thread.Sleep(1000);
    
    		//Console.WriteLine(myLocalObject.Nachname);
    		//Console.WriteLine(myLocalObject.Vorname);
    		//Console.WriteLine(myLocalObject.Telefon);
    		Console.WriteLine("Stop Task");
    	});
    
       
    	Console.WriteLine("Stop");
    	Console.ReadKey();
    	
    	//...
    	
    public class CKontakte
    {
    	public String Nachname;
    	public String Vorname;
    	public String Telefon;
    }

    Thursday, March 14, 2019 6:34 PM

Answers

  • Hi Markus Freitag,

    Yes, if you need more parameters, please use Task.Factory.StartNew.

    And note that, I make a simple example to clarify.

    Task.Run(example) is equal to the code below.

    Task.Factory.StartNew(example, 
        CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

    It uses the default parameters. If you need to define a lot of parameters by yourself, use Task.Factory.StartNew to define the parameters. 

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, March 28, 2019 10:39 AM
    Moderator

All replies

  • Hello,

    To get a understanding between the three I recommend reading the following blog post as it's not a short response.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, March 15, 2019 12:17 AM
    Moderator
  • Hi Markus Freitag,

    Thank you for posting here.

    A quick answer: They are same in your code. 

    For more details, the link provided by Kareninstructor gives a good explanation.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 15, 2019 7:59 AM
    Moderator
  • Hello Karen,
    Thanks for response.
    Yes should be the same.
    Task.Run(someAction);
    that’s exactly equivalent to:
    Task.Factory.StartNew(someAction,
     CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);


    If you can correct the examples like the first one, then I will see the differences and I will understand better.
    Thanks in advance.
    How I get    kontakt, TaskCreationOptions.LongRunning   inside the Task.Run
                 I have here only cancel token.
      
    With best regards Markus

    // With Run I need not the Start
    var tVariant2 = Task.Run(() =>
    {
     //CKontakte myLocalObject = (CKontakte)kontakt;
     //Thread.Sleep(1000);
       
     //Console.WriteLine(myLocalObject.Nachname);
     //Console.WriteLine(myLocalObject.Vorname);
     //Console.WriteLine(myLocalObject.Telefon);
     Console.WriteLine("Stop Task");
    });
    var tVariant3 = Task.Factory.StartNew(() =>
    {
     //CKontakte myLocalObject = (CKontakte)kontakt;
     //Thread.Sleep(1000);
     //Console.WriteLine(myLocalObject.Nachname);
     //Console.WriteLine(myLocalObject.Vorname);
     //Console.WriteLine(myLocalObject.Telefon);
     Console.WriteLine("Stop Task");
    });


    Friday, March 15, 2019 4:37 PM
  • Hi Markus Freitag,

    >>How I get    kontakt, TaskCreationOptions.LongRunning   inside the Task.Run

    According to your description, tVariant2 and tVariant3 are same.

    But for TaskCreationOptions.LongRunning, please note, the biggest difference between the two functions is that Task.Factory.StartNew can set the thread to run for a long time, then the thread pool will not wait for this thread to reclaim. So you need to use Task.Factory.StartNew if you need to set the thread to run for a long time; Otherwise, use Task.Run.

    In fact, Task.Run can be thought of as a wrapper around Task.Factory.StartNew, using simple default parameters. If you need to define a lot of parameters by yourself, use Task.Factory.StartNew to define the parameters.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 18, 2019 7:19 AM
    Moderator
  • Hello Wendy,

    var tVariant2 = Task.Run(() =>
    {
     //CKontakte myLocalObject = (CKontakte)kontakt;
     //Thread.Sleep(1000);

    Can not I pass the object kontakt into the task?

    Then that would be the difference? Am I correct?

    Best regards, Markus

    Monday, March 18, 2019 11:36 AM
  • Hi Markus Freitag,

    The way you used could get what you want.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 20, 2019 7:23 AM
    Moderator
  • Hello Wendy,
    >The way you used could get what you want.
    Nice answer, but my question was is it possible yes or no.
    var tVariant3 = Task.Factory.StartNew(() =>
    var tVariant2 = Task.Run(() =>
    I thought that was outdated, better use the new one with Task.Run.
    Maybe I can not hand over an object, that's what I wanted to know.
    Best regards, Markus
    Wednesday, March 20, 2019 5:45 PM
  • Hi Markus Freitag,

    In my previous reply, I shows when to use Task.Run and when to use Task.Factory.StartNew.

    Normally, we use Task.Run. But if you need some parameters, Task.Factory.StartNew is a good choice.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 27, 2019 9:30 AM
    Moderator
  • Normally, we use Task.Run. But if you need some parameters, Task.Factory.StartNew is a good choice.


    Thanks. So if I need some paramaters is with Task.Run not possible.

    If so, we can close the thread.

    Greeting Markus

    Wednesday, March 27, 2019 5:16 PM
  • Hi Markus Freitag,

    Yes, if you need more parameters, please use Task.Factory.StartNew.

    And note that, I make a simple example to clarify.

    Task.Run(example) is equal to the code below.

    Task.Factory.StartNew(example, 
        CancellationToken.None, TaskCreationOptions.DenyChildAttach, TaskScheduler.Default);

    It uses the default parameters. If you need to define a lot of parameters by yourself, use Task.Factory.StartNew to define the parameters. 

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, March 28, 2019 10:39 AM
    Moderator