Passing parameter into a Task.Factory.StartNew

后端 未结 3 2117
借酒劲吻你
借酒劲吻你 2021-02-20 05:38

Given the following code:

string injectedString = \"Read string out of HttpContext\";
Task.Factory.StartNew(() =>
 {
    MyClass myClass = new MyClass();
             


        
相关标签:
3条回答
  • 2021-02-20 05:53

    Your lambda will be hoisted out into a compiler generated class. The injectedString variable will become a field of that class.

    So, it will be garbage collected when the generated class is out of scope (which is basically at the very end of your lambda), and the GC decides to perform a collection.

    In response to your comment:

    There is no duplication. The compiler turns this:

     string injectedString = "Read string out of HttpContext";
     Task.Factory.StartNew(() =>
     {
        MyClass myClass = new MyClass();
        myClass.Method(injectedString);
     }
    

    Into this:

    CompilerGeneratedClass c1 = new CompilerGeneratedClass();
    c1.injectedString = "Read string out of HttpContext";
    // call delegate here.
    

    Remember also: Strings are interned in the CLR. Even if the code was duplicated.. string literals will be interned in a pool. You would essentially only have a native WORD sized reference duplicated that pointed at the string (string literals only..)

    0 讨论(0)
  • 2021-02-20 05:59

    If you're concerned about injectedString may be "garbage collected" before myClass.Method(injectedString); runs ?

    Answer is no. You don't need to worry about that, because injectedString is no more a local variable when it is closed in the lambda. It will become a field in new compiler generated class.

    If you're concerned about will the garbage collector collect it at right time? Answer is yes, It will do when the instance of that class goes out of scope and no refernces to it in managed code. That would happen certainly after Task gets completed and gets out of scope.

    0 讨论(0)
  • 2021-02-20 06:15

    You should probably use the Task.Factory.StartNew(Action<object> action, object state) overload to pass state into your new task.

    Task.Factory.StartNew((object myState) => {
        var i = (int)myState;
    
        //Do calculations...
        var x = i + 10; 
    }, 10);
    
    0 讨论(0)
提交回复
热议问题