C# 许多长时间运行的任务,IAsyncResult回调都发生在最后
我已经拔头发好几个星期了,我需要一些帮助。这是一个SharePoint实用程序,用于将RDL文件复制到项目站点。究竟为什么在处理整个列表后调用我的IAsyncResult.EndInvokes?平均而言,我可以在10秒内将所有RDL文件复制到一个项目站点。考虑到这一点,我希望在一分钟内达到IAsyncResult方法中的断点。相反,直到所有报告都复制到所有项目之后,断点才会命中,然后断点会连续快速命中。我做错了什么 我只能使用.NET 3.5,因此点击处理不是一个选项:( 代表:C# 许多长时间运行的任务,IAsyncResult回调都发生在最后,c#,multithreading,sharepoint,C#,Multithreading,Sharepoint,我已经拔头发好几个星期了,我需要一些帮助。这是一个SharePoint实用程序,用于将RDL文件复制到项目站点。究竟为什么在处理整个列表后调用我的IAsyncResult.EndInvokes?平均而言,我可以在10秒内将所有RDL文件复制到一个项目站点。考虑到这一点,我希望在一分钟内达到IAsyncResult方法中的断点。相反,直到所有报告都复制到所有项目之后,断点才会命中,然后断点会连续快速命中。我做错了什么 我只能使用.NET 3.5,因此点击处理不是一个选项:( 代表: public
public delegate string CopyReportsParallel(string targetSiteUrl, List<SourceReportInfo> reports, EditorSettings settings);
这是我的回调方法。理想情况下,我希望有尽可能多的线程进行异步处理,并在完成后立即报告,这样我就可以用进度条更新网页
private static void BackgroundWorkerIsDone(IAsyncResult ar)
{
CopyReportsParallel d = (CopyReportsParallel)ar.AsyncState;
string result = d.EndInvoke(ar);
lock ((_lockObject))
{
_completedCount += 1;
if (logTheHistory)
{
int completedPercent = _completedCount * 100 / _totalCount;
string report = result + Environment.NewLine + "Update - " + completedPercent.ToString() + "% Complete " + " # of Projects Completed: " + _completedCount.ToString();
bw.ReportProgress(completedPercent, report);
}
if (_completedCount == _totalCount)
{
waitHandle.Set();
}
}
}
并行复制逻辑(每个请求):
私有静态字符串ParallelCopyLogic(字符串targetSiteUrl、列表报告、编辑器设置)
{
字符串_runDetail=“”;
_runDetail++=(System.DateTime.Now.ToString()+“-正在处理项目:“+targetSiteUrl+Environment.NewLine”);
使用(_reportSvc=new reportingservice 2010())
{
_reportSvc.Url=reportSvcURl;
_reportSvc.Credentials=System.Net.CredentialCache.DefaultCredentials;
_reportSvc.PreAuthenticate=true;
_reportSvc.Timeout=Timeout.Infinite;
使用(SPSite targetSite=new SPSite(targetSiteUrl))
使用(SPWeb targetWeb=targetSite.OpenWeb())
{
foreach(报告中的SourceReportInfo reportInfo)
{
尝试
{
SPDocumentLibrary targetReportLibrary=targetWeb.List[reportInfo.SourceReport.ParentList.RootFolder.Name]作为SPDocumentLibrary;
SPFile targetReportFile=targetReportLibrary.RootFolder.Files.Add(reportInfo.sourereportrl、reportInfo.SourceReportFileBytes、reportInfo.SourceReport.Properties,true);
SPListItem targetReport=targetReportFile.GetListItem();
字符串sourceURL=reportInfo.SourceReport.Web.Url+“/”+reportInfo.SourceReport.Url;
字符串destinationURL=targetReport.Web.Url+“/”+targetReport.Url;
字符串targetListUrl=targetReport.Web.Url+“/”+targetReport.ParentList.RootFolder.Name;
bool-dsUpdated=false;
bool=false;
bool-ddUpdated=false;
如果(!targetReport.HasUniqueRoleAssignments)
targetReport.BreakRoleInheritance(真);
foreach(reportInfo.roleasignments中的SPRoleAssignment角色)
targetReport.RoleAssignments.Add(角色);
targetReport.Update();
//更新复制的项目
if(reportInfo.SoureReportUrl.EndsWith(“.rdl”))
{
SPSecurity.RunWithElevatedPrivileges(新的SPSecurity.CodeToRunElevated(delegate())
{
ReportService.DataSource[]sourceDataSources=\u reportSvc.GetItemDataSources(sourceURL);
ReportService.DataSource[]destDataSources=\u reportSvc.GetItemDataSources(destinationURL);
for(int i=0;i private static void BackgroundWorkerIsDone(IAsyncResult ar)
{
CopyReportsParallel d = (CopyReportsParallel)ar.AsyncState;
string result = d.EndInvoke(ar);
lock ((_lockObject))
{
_completedCount += 1;
if (logTheHistory)
{
int completedPercent = _completedCount * 100 / _totalCount;
string report = result + Environment.NewLine + "Update - " + completedPercent.ToString() + "% Complete " + " # of Projects Completed: " + _completedCount.ToString();
bw.ReportProgress(completedPercent, report);
}
if (_completedCount == _totalCount)
{
waitHandle.Set();
}
}
}
private static string ParallelCopyLogic(string targetSiteUrl, List<SourceReportInfo> reports, EditorSettings settings)
{
string _runDetail = "";
_runDetail += (System.DateTime.Now.ToString() + " - Working on Project: " + targetSiteUrl + Environment.NewLine);
using (_reportSvc = new ReportingService2010())
{
_reportSvc.Url = reportSvcURl;
_reportSvc.Credentials = System.Net.CredentialCache.DefaultCredentials;
_reportSvc.PreAuthenticate = true;
_reportSvc.Timeout = Timeout.Infinite;
using (SPSite targetSite = new SPSite(targetSiteUrl))
using (SPWeb targetWeb = targetSite.OpenWeb())
{
foreach (SourceReportInfo reportInfo in reports)
{
try
{
SPDocumentLibrary targetReportLibrary = targetWeb.Lists[reportInfo.SourceReport.ParentList.RootFolder.Name] as SPDocumentLibrary;
SPFile targetReportFile = targetReportLibrary.RootFolder.Files.Add(reportInfo.SoureReportUrl, reportInfo.SourceReportFileBytes, reportInfo.SourceReport.Properties, true);
SPListItem targetReport = targetReportFile.GetListItem();
string sourceURL = reportInfo.SourceReport.Web.Url + "/" + reportInfo.SourceReport.Url;
string destinationURL = targetReport.Web.Url + "/" + targetReport.Url;
string targetListUrl = targetReport.Web.Url + "/" + targetReport.ParentList.RootFolder.Name;
bool dsUpdated = false;
bool paramUpdated = false;
bool ddUpdated = false;
if (!targetReport.HasUniqueRoleAssignments)
targetReport.BreakRoleInheritance(true);
foreach (SPRoleAssignment role in reportInfo.RoleAssignments)
targetReport.RoleAssignments.Add(role);
targetReport.Update();
// Update copied Items
if (reportInfo.SoureReportUrl.EndsWith(".rdl"))
{
SPSecurity.RunWithElevatedPrivileges(new SPSecurity.CodeToRunElevated(delegate()
{
ReportService.DataSource[] sourceDataSources = _reportSvc.GetItemDataSources(sourceURL);
ReportService.DataSource[] destDataSources = _reportSvc.GetItemDataSources(destinationURL);
for (int i = 0; i < sourceDataSources.Length; i++)
{
DataSourceReference sourceDs = sourceDataSources[i].Item as DataSourceReference;
DataSourceDefinition sourceDef = sourceDataSources[i].Item as DataSourceDefinition;
DataSourceDefinition destDef = destDataSources[i].Item as DataSourceDefinition;
// Updates the target data source references
if (sourceDs != null && sourceDs.Reference != null && sourceDataSources[i].Name == destDataSources[i].Name)
{
string sourceDsName = System.IO.Path.GetFileName(sourceDs.Reference);
destDataSources[i].Item = new DataSourceReference { Reference = targetListUrl + "/" + sourceDsName };
dsUpdated = true;
}
//Updates the target data source definitions
else if (sourceDef != null && destDef != null && sourceDataSources[i].Name == destDataSources[i].Name)
{
//Set data source credentials
destDef.Enabled = true;
destDef.CredentialRetrieval = sourceDef.CredentialRetrieval;
destDef.ImpersonateUser = sourceDef.ImpersonateUser;
destDef.WindowsCredentials = sourceDef.WindowsCredentials;
if (settings != null && destDef.CredentialRetrieval == CredentialRetrievalEnum.Store)
{
destDef.UserName = settings.DataAccessUserName;
settings.GetDataAccessPassword(destDef);
}
destDataSources[i].Item = destDef;
ddUpdated = true;
}
}
if (dsUpdated | ddUpdated)
{
_reportSvc.SetItemDataSources(destinationURL, destDataSources);
dsUpdated = false;
ddUpdated = false;
}
//Updates the target report's project code default parameter
ItemParameter[] parameters = _reportSvc.GetItemParameters(destinationURL, null, false, null, null);
foreach (ItemParameter item in parameters)
{
//Have to check if prompt text is empty as this API doesnt expose hidden property
if (item.Name.ToLower() == "projectcode" && string.IsNullOrEmpty(item.Prompt) && item.DefaultValues.Length == 1)
{
item.DefaultValues[0] = targetWeb.Name;
paramUpdated = true;
break;
}
}
if (paramUpdated)
{
_reportSvc.SetItemParameters(destinationURL, parameters);
paramUpdated = false;
}
}));
}
else if (reportInfo.SoureReportUrl.EndsWith(".rsds"))
{
//Update Shared Datasets
DataSourceDefinition sourceDef = _reportSvc.GetDataSourceContents(sourceURL);
DataSourceDefinition destinationDef = _reportSvc.GetDataSourceContents(destinationURL);
destinationDef.Enabled = true;
destinationDef.CredentialRetrieval = sourceDef.CredentialRetrieval;
destinationDef.ImpersonateUser = sourceDef.ImpersonateUser;
destinationDef.WindowsCredentials = sourceDef.WindowsCredentials;
if (settings != null && destinationDef.CredentialRetrieval != CredentialRetrievalEnum.Integrated)
{
destinationDef.UserName = settings.DataAccessUserName;
settings.GetDataAccessPassword(destinationDef);
}
_reportSvc.SetDataSourceContents(destinationURL, destinationDef);
}
_runDetail += (System.DateTime.Now.ToString() + " - Finished updating report: " + targetReport.Name.ToString() + Environment.NewLine);
}
catch (Exception ex)
{
_runDetail += (System.DateTime.Now.ToString() + " - There was an error on project: " + targetSiteUrl + Environment.NewLine);
throw new InvalidOperationException(string.Format("Error while updating report {0} on Project Site {1}. Exception:{2}", reportInfo.SoureReportUrl, targetSiteUrl, ex.Message), ex);
}
}
}
}
return _runDetail;
}