许多长时间运行的任务,IAsyncResult回调发生在最后

尼克·H

我已经把头发拉了好几个星期了,我需要一些帮助。这是一个SharePoint实用程序,可将RDL文件复制到项目站点。为什么在处理完整个列表后调用IAsyncResult.EndInvokes?平均而言,我可以在10秒内将所有RDL文件复制到1个项目站点。考虑到这一点,我希望在一分钟内在IAsyncResult方法内达到断点。取而代之的是,直到将所有报表都复制到所有项目中后,断点才会命中,然后断点会迅速触发。我在这里做错了什么?

我只能使用.NET 3.5,因此TAP处理不是一种选择:(

代表:

public delegate string CopyReportsParallel(string targetSiteUrl, List<SourceReportInfo> reports, EditorSettings settings);

此方法编译RDL和目标项目站点的列表。对于每个目标站点,创建一个代表以处理复制逻辑的委托:

private static void bw_DoWork(Object sender, DoWorkEventArgs e)
    {            
        int workerIndex = bwObject.targetProjectSiteUrls.Count;
        _totalCount = bwObject.targetProjectSiteUrls.Count;
    foreach (string s in bwObject.targetProjectSiteUrls)
    {
        if (cancelled)
            break;

        //Get a delegate for the operation
        CopyReportsParallel d = new CopyReportsParallel(ParallelCopyLogic);
        IAsyncResult ar = d.BeginInvoke(s, bwObject.reports, bwObject.settings, BackgroundWorkerIsDone, d);               
    }

    waitHandle.WaitOne();

}

这是我的回叫方法。理想情况下,我希望尽可能多的线程异步处理并在完成后立即报告,以便可以使用进度条更新网页。

    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;
        }

请让我知道您的想法或您是否需要更多信息。我看了又看又看,所以我不想让任何人“在这里做作业”。我想从一些经验丰富的程序员那里学习一两本书。

谢谢!

用户3306518

这种逻辑没有错。编译器将触发ASYNC委托,同时接收其IASYNC回调。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

苹果允许后台任务运行多长时间?

来自分类Dev

at(1) 等待计划任务完成多长时间?

来自分类Dev

*演员多长时间

来自分类Dev

花费多长时间?

来自分类Dev

长时间运行的任务或线程?

来自分类Dev

iOS:长时间运行的任务

来自分类Dev

使用RxJava(ReactiveX)运行Observable需要多长时间?

来自分类Dev

如何检查进程运行了多长时间?

来自分类Dev

预测scikit学习分类将运行多长时间

来自分类Dev

在Python中运行循环需要多长时间?

来自分类Dev

我的Linux系统已经运行了多长时间?

来自分类Dev

如何检查进程运行了多长时间?

来自分类Dev

测量程序运行多长时间

来自分类Dev

Bash:计算脚本运行需要多长时间

来自分类Dev

Erlang:如何处理长时间运行的init回调?

来自分类Dev

任务并行库和长时间运行的任务

来自分类Dev

编译minix需要多长时间?

来自分类Dev

测试套件需要多长时间?

来自分类Dev

报告崩溃需要多长时间?

来自分类Dev

Net:http封锁了多长时间

来自分类Dev

训练pyBrain多长时间?

来自分类Dev

处理请求需要多长时间

来自分类Dev

TopicClient应该生存多长时间?

来自分类Dev

编程操作需要多长时间?

来自分类Dev

金额少于200多长时间

来自分类Dev

编程操作需要多长时间?

来自分类Dev

Cygwin加载需要多长时间?

来自分类Dev

NTP“同步”需要多长时间

来自分类Dev

Net:http封锁了多长时间