C# Task.Factory.StartNew中arg的值是多少
我有C# Task.Factory.StartNew中arg的值是多少,c#,multithreading,C#,Multithreading,我有Task.Factory.StartNew和args参数。我不太明白arg的值是多少。那么,args将是什么值,以及如何确定该值 int count = table.Rows.Count; Dictionary<int, string> expDic = new Dictionary<int, string>(); List<Task> taskList = new List<Task>(); for (int f
Task.Factory.StartNew
和args
参数。我不太明白arg
的值是多少。那么,args
将是什么值,以及如何确定该值
int count = table.Rows.Count;
Dictionary<int, string> expDic = new Dictionary<int, string>();
List<Task> taskList = new List<Task>();
for (int f = 0; f < count; f++)
{
DataRow tempformula = table.Rows[f];
string formulaSymbol = Convert.ToString(tempformula[Common.Systems.Sustainability.Constants.IndicatorFormula.Symbol]);
var t = Task.Factory.StartNew(new Action<object>((args) => {
int i = (int)args;
_sem.Wait();
StringBuilder expression = new StringBuilder(1024);
DataRow formula = table.Rows[i];
int indID = Convert.ToInt32(formula[Common.Systems.Sustainability.Constants.IndicatorFormula.IndicatorID]);
int sequence = Convert.ToInt32(formula[Common.Systems.Sustainability.Constants.IndicatorFormula.Sequence]);
int? referenceIndID = Common.Util.TryToConvertToInt32(formula, Common.Systems.Sustainability.Constants.IndicatorFormula.ReferenceIndicatorID);
decimal? val = Common.Util.TryToConvertToDecimal(formula, Common.Systems.Sustainability.Constants.IndicatorFormula.Value);
string symbol = Convert.ToString(formula[Common.Systems.Sustainability.Constants.IndicatorFormula.Symbol]);
string formulaOutputUnit = Convert.ToString(formula[Common.Systems.Sustainability.Constants.Indicator.Unit]);
if (!string.IsNullOrWhiteSpace(callerAcceptedUnit)) // added by HC on 2016-05-27
{
formulaOutputUnit = callerAcceptedUnit;
}
if (referenceIndID == null)
{
if (val == null)
{
DataSet indicator = GetIndicatorByIDFromCache(companyID, indID); // added by HC on 2017-05-12, for performance tuning.
//using (DataSet indicator = indicatorMgr.GetIndicatorByID(companyID, indID))
{
DataRow dr = indicator.Tables[0].Rows[0];
int indicatorType = Convert.ToInt32(dr[Common.Systems.Sustainability.Constants.Indicator.IndicatorType]);
if (indicatorType == (int)Common.Systems.Sustainability.Constants.IndicatorTypes.Currency
|| indicatorType == (int)Common.Systems.Sustainability.Constants.IndicatorTypes.Numeric)
{
decimal? total = SumTotal(companyID, indID, indicatorType, fromDate, toDate, formulaOutputUnit, callerIndicatorID, breakToMonthly, specifiedLocations, approvalStatus
, usageDataSet, interval
, indicatorIDsChain
, allowNull
);
expression.Append(total.ToString());
}
else if (symbol == "+"
|| symbol == "-"
|| symbol == "*"
|| symbol == "/")
{
expression.Append(symbol);
}
}
}
else
{
expression.Append(val.ToString());
}
}
else
{
string exp = GetExpression(companyID, fromDate, toDate, referenceIndID.Value.ToString(), indID.ToString(), formulaOutputUnit, breakToMonthly, specifiedLocations, approvalStatus
, usageDataSet, interval
, indicatorIDsChain
);
//expression.Append(exp);
using (DataTable dt = new DataTable())
{
// fault tolerance, for in-case users assigned symbol in the last line of formula
if (exp.EndsWith("+")
|| exp.EndsWith("-")
|| exp.EndsWith("*")
|| exp.EndsWith("/"))
{
exp = exp.Substring(0, exp.Length - 1);
}
object result = dt.Compute(exp, "");
expression.Append(result);
}
} // end if (referenceIndID == null)...
if ("IF".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| "THEN".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| "ELSE IF".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| "ELSE".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| ">".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| ">=".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| "=".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| "<=".Equals(symbol, StringComparison.OrdinalIgnoreCase)
|| "<".Equals(symbol, StringComparison.OrdinalIgnoreCase)
)
{
//--------------------------------------------------------
// Begin, added by HC on 2016-12-14
// as requested by CQS users, add "IF" statement for GRI indicators
string exp = HandleIFCondition(table, i, companyID, fromDate, toDate, indicatorIDs, callerIndicatorID, callerAcceptedUnit, breakToMonthly, specifiedLocations, approvalStatus, usageDataSet, interval, indicatorIDsChain);
expression.Append(exp);
// End, added by HC on 2016-12-14
//--------------------------------------------------------
// Find End if, added by Alex Poon on 20190306
int ifIndex = i;
for (var j = ifIndex; j < count; j++)
{
string endIfSymbol = Convert.ToString(table.Rows[j][Common.Systems.Sustainability.Constants.IndicatorFormula.Symbol]);
if ("END IF".Equals(endIfSymbol, StringComparison.OrdinalIgnoreCase))
{
i = j;
break;
}
}
}
else
{
if (!string.IsNullOrWhiteSpace(symbol)
&& expression.Length > 0 // added by HC on 2016-06-24
)
{
if (i + 1 >= count && symbol != ")")
{
// ignore the symbol
}
else if (expression.ToString() != symbol)
{
expression.Append(symbol);
}
}
else if (expression.Length == 0 && symbol == "(")
{
expression.Append(symbol);
}
if (symbol == "days")
{
// added by HC on 2017-01-23, a new requirements for calculating average values by days.
if (fromDate != null && toDate != null)
{
int days = toDate.Value.Subtract(fromDate.Value).Days + 1;
expression.Append(days);
}
else
{
expression.Append(1);
}
}
} // end if ("IF".Equals(symbol, StringComparison.OrdinalIgnoreCase)...
if (expDic != null && expression != null)
{
expDic.Add(i, expression.ToString());
}
_sem.Release();
}), f);
taskList.Add(t);
if ("IF".Equals(formulaSymbol, StringComparison.OrdinalIgnoreCase))
{
for (var j = f; j < count; j++)
{
string endIfSymbol = Convert.ToString(table.Rows[j][Common.Systems.Sustainability.Constants.IndicatorFormula.Symbol]);
if ("END IF".Equals(endIfSymbol, StringComparison.OrdinalIgnoreCase))
{
f = j;
break;
}
}
}
}
Task.WaitAll(taskList.ToArray());
List<int> keys = expDic.Keys.ToList();
keys.Sort();
string finalExpression = "";
foreach (var key in keys)
{
finalExpression += expDic[key];
}
return finalExpression;
}
int count=table.Rows.count;
Dictionary expDic=新字典();
List taskList=新列表();
对于(int f=0;f{
int i=(int)args;
_sem.Wait();
StringBuilder表达式=新的StringBuilder(1024);
DataRow公式=表.行[i];
int indID=Convert.ToInt32(公式[Common.Systems.Sustainability.Constants.IndicatorFormula.indicator]);
int sequence=Convert.ToInt32(公式[Common.Systems.Sustainability.Constants.IndicatorFormula.sequence]);
int?referenceIndID=Common.Util.TryToConvertToInt32(公式,Common.Systems.Sustainability.Constants.IndicatorFormula.ReferenceIndicatorID);
decimal?val=Common.Util.TryToDecimal(公式,Common.Systems.Sustainability.Constants.IndicatorFormula.Value);
string symbol=Convert.ToString(公式[Common.Systems.Sustainability.Constants.IndicatorFormula.symbol]);
string formulaOutputUnit=Convert.ToString(公式[Common.Systems.Sustainability.Constants.Indicator.Unit]);
如果(!string.IsNullOrWhiteSpace(callerAcceptedUnit))//由HC于2016年5月27日添加
{
formulaOutputUnit=callerAcceptedUnit;
}
if(referenceIndID==null)
{
if(val==null)
{
DataSet indicator=GetIndicatorByIDFromCache(companyID,indID);//由HC于2017年5月12日添加,用于性能调整。
//使用(数据集指示符=indicatorMgr.GetIndicatorByID(companyID,indID))
{
DataRow dr=指示符。表[0]。行[0];
int indicatorType=Convert.ToInt32(dr[Common.Systems.Sustainability.Constants.Indicator.indicatorType]);
if(indicatorType==(int)Common.Systems.Sustainability.Constants.IndicatorTypes.Currency
||indicatorType==(int)Common.Systems.Sustainability.Constants.IndicatorTypes.Numeric)
{
decimal?total=总和(公司ID、独立ID、指示符类型、fromDate、toDate、formulaOutputUnit、callerIndicatorID、breakToMonthly、指定位置、批准状态)
,usageDataSet,间隔
,指示链
,allowNull
);
expression.Append(total.ToString());
}
否则如果(符号=“+”
||符号=“-”
||符号=“*”
||符号==“/”)
{
表达式。追加(符号);
}
}
}
其他的
{
expression.Append(val.ToString());
}
}
其他的
{
string exp=GetExpression(公司ID、fromDate、toDate、referenceIndID.Value.ToString()、indID.ToString()、formulaOutputUnit、breakToMonthly、specifiedLocations、approvalStatus
,usageDataSet,间隔
,指示链
);
//表达式.Append(exp);
使用(DataTable dt=newdatatable())
{
//容错性,适用于用户在公式最后一行指定符号的情况
如果(exp.EndsWith(“+”)
||exp.EndsWith(“-”)
||exp.EndsWith(“*”)
||exp.EndsWith(“/”)
{
exp=exp.Substring(0,exp.Length-1);
}
对象结果=dt.Compute(exp,“”);
表达式。追加(结果);
}
}//如果(referenceIndID==null)结束。。。
if(“if.”等于(符号,StringComparison.OrdinalIgnoreCase)
||“THEN”.Equals(符号,StringComparison.OrdinalIgnoreCase)
||“ELSE IF”.Equals(符号,StringComparison.OrdinalIgnoreCase)
||“ELSE”.Equals(符号,StringComparison.OrdinalIgnoreCase)
||“>”.Equals(符号,StringComparison.OrdinalIgnoreCase)
||“>=”.Equals(符号,StringComparison.OrdinalIgnoreCase)
||“=”.Equals(符号,StringComparison.OrdinalIgnoreCase)
||“args
在您的情况下是将传递给操作
参数指定的委托的数据
看
将StartNew
重载与Action
一起使用时,需要提供类型为object
的下一个参数,该参数将在传递的委托中使用
Task.Factory.StartNew((o) => {/* 1 will be used here */}, 1, token, options, scheduler);
args参数(在文档中称为“state”)是调用Task.Factory.StartNew
时需要指定的参数
状态(对象):包含操作委托使用的数据的对象
Task.Factory.StartNew((o) => {/* 1 will be used here */}, 1, token, options, scheduler);
样本:
object param = somevalue;
Task.Factory.StartNew( (arg) =>
{ /* get the arg value here.*/
}, param);
您尚未提供完整的示例。如果您使用操作调用TaskFactory.StartNew
,则必须提供第二个参数,该参数是随后传递给该委托的值。我发现没有调用TaskFactory.StartNew,似乎只有我们