Home > Net >  Consult the Task how to incoming parameters? Or what's the improvement way
Consult the Task how to incoming parameters? Or what's the improvement way

Time:10-14

int a=1;
Task. Run (()=& gt;
{
Console. WriteLine (a);//call external value
Thread.sleep (2000);
Console. WriteLine (a);//called again after dormancy
});

Thread.sleep (1000);
A=12;//value changed after dormancy
The Console. ReadKey ();
The above code, after external parameters change, the Task threads at run time, also be affected, output respectively two kinds of results,

The object a="a";
ParameterizedThreadStart PTS=new ParameterizedThreadStart ((fileName)=& gt;
{
Console. WriteLine (fileName);
Thread.sleep (2000);
Console. WriteLine (fileName);
});
Thread OpenFileTheard=new Thread (PTS);
OpenFileTheard. Start (a);
Thread.sleep (1000);
A="b".
The incoming parameters using Thread this way, the Thread is not affected, the output is not affected
Would you please tell me what the way of Task can be used like Thread? Is not affected by external variables change and be

CodePudding user response:

Task. Run (()=& gt;
{
Int b=a;
Console. WriteLine (b);//call external value
Thread.sleep (2000);
Console. WriteLine (b);//called again after dormancy
});

This value object, in addition to copy a value you don't have to

Or Task. Run (()=& gt; Call (a));

CodePudding user response:

 object aa="c"; 
Task. Run (()=& gt; ActionX (aa));

Thread.sleep (1000);
Aa=12;//
The Console. ReadKey ();


 private static void ActionX (object fileName) 
{
Console. WriteLine (fileName);
Thread.sleep (2000);
Console. WriteLine (fileName);
}

CodePudding user response:

Your task is to call the external variables,
Your thread is used into the ginseng, certainly is not the same, is a copy of the value type transmission parameters,
 static void Main (string [] args) 
{
Int a=10;
Task. Factory. StartNew (x=& gt; {
X=(int) x + 10;
Console. WriteLine (x);
}, a);
Thread.sleep (1000);
Console. WriteLine (a);
The Console. ReadKey ();
}

CodePudding user response:

refer to the second floor hanghangz response:
 object aa="c"; 
Task. Run (()=& gt; ActionX (aa));

Thread.sleep (1000);
Aa=12;//
The Console. ReadKey ();


 private static void ActionX (object fileName) 
{
Console. WriteLine (fileName);
Thread.sleep (2000);
Console. WriteLine (fileName);
}
so!!! Use method of no pollution to the data, what is the specific reason, please! Why can't directly write

CodePudding user response:

Closures are an outstanding feature of modern programming language, and in this you become pollution data

CodePudding user response:

Difference is that one is the parameters passed, one is the direct use of a thing

CodePudding user response:

reference 5 floor stherix reply:
closure is an outstanding feature of modern programming language, and in this you become pollution data
fledglings programming gate, a great god forgive the

CodePudding user response:

Task. Factory. StartNew (
P=& gt; {}, 1);

CodePudding user response:

reference 4 floor qq_40118399 response:
Quote: refer to the second floor hanghangz response:

 object aa="c"; 
Task. Run (()=& gt; ActionX (aa));

Thread.sleep (1000);
Aa=12;//
The Console. ReadKey ();


 private static void ActionX (object fileName) 
{
Console. WriteLine (fileName);
Thread.sleep (2000);
Console. WriteLine (fileName);
}
so!!! Use method of no pollution to the data, what is the specific reason, please! Why can't directly write



Basically it is this version of menstrual problems, to understand the scope of object, at the same time have a good understanding of gc,
A scope, a gc, there are people in almost every week

CodePudding user response:

See also: https://technet.microsoft.com/zh-cn/windowsserver/dd321456 (v=vs. 90) state
  •  Tags:  
  • C#
  • Related