以下是提高打开多个多线程的新方法的性能的代码,但在单线程上调用的方法具有良好的性能。
那么请你告诉我如何最小化在多线程上运行的新方法的时间,而不是在单线程上运行的方法(即Old方法)
public List<CEntity> GetSomeFunction(Ent ent)
{
List<CEntity> lstCE = new List<CEntity>();
this.objEnt = ent;
TestObj objTempBal = new objTempBal();
objTempBal.objEnt = ent;
System.Diagnostics.Debug.WriteLine("new Method");
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
objTempBal.FetchSomeDataAsync(ent).Wait();
stopWatch.Stop();
System.Diagnostics.Debug.WriteLine(String.Format("Elapsed time: {0} -- {1} ",
stopWatch.Elapsed.Milliseconds, DateTime.Now));
System.Diagnostics.Debug.WriteLine("old Method");
Stopwatch stopWatch1 = new Stopwatch();
stopWatch1.Start();
List<CEntity> result = null;
ResponseApi response = null;
try
{
IWrapper Wrapper = InitializeWrapper(ent);
int pageNo = 1;
bool createPaging = true;
while (createPaging)
{
SearchCriteria objSC = new SearchCriteria()
{
//pageSize = this.pageSize,
pageSize = "1000",
page = pageNo.ToString()
};
response = Wrapper.SomeSynchronusAPIMethod(objSC);
if (response.Success)
{
result = (List<CEntity>)response.results;
if (result.Count == 0)
{
createPaging = false;
}
else
{
result = result.ToList();
}
lstCE.AddRange(result);
}
else { createPaging = false; }
pageNo = pageNo + 1;
}
}
catch (Exception ex)
{
throw;
}
stopWatch1.Stop();
System.Diagnostics.Debug.WriteLine(String.Format("Elapsed time: {0} -- {1} ",
stopWatch1.Elapsed.Milliseconds, DateTime.Now));
return lstCE;
}
public async Task<List<CEntity>> FetchSomeDataAsync(Ent ent)
{
var tasks = new List<Task<List<CEntity>>>();
int pageCount = 18;
List<int> pages = new List<int>();
for (int addItemToList = 1; addItemToList <= pageCount; addItemToList++)
{
pages.Add(addItemToList);
}
List<CEntity> resultRange = new List<CEntity>();
foreach (var list in pages.Batch(10))
{
foreach (var item in list)
{
IWrapper Wrapper1 = InitializeWrapper(ent);
tasks.Add(Task.Run(() => FetchSomeDataFromSynchronusAPI(Wrapper1, item)));
}
var results = await Task.WhenAll(tasks);
resultRange.AddRange(results.SelectMany(x => x.ToList()).ToList());
tasks = new List<Task<List<CEntity>>>();
}
return resultRange;
}
public async Task<List<CEntity>> FetchSomeDataFromSynchronusAPI(IWrapper Wrapper, int pageNo)
{
System.Diagnostics.Debug.WriteLine("Start pageNo " + pageNo + " -- " + DateTime.Now);
List<CEntity> result = null;
ResponseApi response = null;
SearchCriteria objSC = new SearchCriteria()
{
pageSize = "1000",
page = pageNo.ToString()
};
response = Wrapper.SomeSynchronusAPIMethod(objSC);
if (response.Success)
{
result = (List<CEntity>)response.results;
result = result.ToList();
}
System.Diagnostics.Debug.WriteLine("End pageNo " + pageNo + " -- " + DateTime.Now);
return await Task.FromResult<List<CEntity>>(result);
//return (result);
}
答案 0 :(得分:2)
无需等待每批。运行所有任务,等待页面外的所有任务.Batch循环。
List<CEntity> resultRange = new List<CEntity>();
foreach (var list in pages.Batch(10))
{
foreach (var item in list)
{
IWrapper Wrapper1 = InitializeWrapper(ent);
tasks.Add(Task.Run(() => FetchSomeDataFromSynchronusAPI(Wrapper1, item)));
}
}
var results = await Task.WhenAll(tasks);
resultRange.AddRange(results.SelectMany(x => x.ToList()).ToList());