Passing parameter into a Task.Factory.StartNew

后端 未结 3 2119
借酒劲吻你
借酒劲吻你 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 06:15

    You should probably use the Task.Factory.StartNew(Action 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);
    

    提交回复
    热议问题