本文介绍了Parallel.ForEach在执行共享点库文件时返回不一致的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ClientContext clientContext =新的ClientContext(siteUrl);
            clientContext.Credentials = new NetworkCredential("username","password");
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = @"< View Scope ='RecursiveAll'>
                                     < Query>
                                     </Query>
                                 </View>" ;;;
            camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();

  Parallel.ForEach(listItems,item =>
            {
                  if(item.FileSystemObjectType == FileSystemObjectType.File)
                    {

ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new NetworkCredential("username","password");
            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
                                     <Query>
                                     </Query>
                                 </View>";
            camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();

  Parallel.ForEach(listItems, item =>
            {
                 if (item.FileSystemObjectType == FileSystemObjectType.File)
                    {

如果库中有3个文件夹,而每个文件夹中有2个文件,则当我使用时,它会跳过随机文件夹中的文件Parallel.ForEach,如果使用普通的foreach,则适用于所有文件夹和文件.

 if library is having 3 folders and each folder having 2 files then it is skipping files for random folder when I use   Parallel.ForEach , If use normal foreach it works fine for all folders and files.

但是我可能有100个文件,因此为了提高性能,我需要使用多线程,以使执行速度更快.

But I may have 100's of files so for performance I need to use multi threading so that execution get faster.

还有其他提高性能的方法吗?

Is there any other way to improve performance ?

SE

推荐答案

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ConsoleAppTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string siteUrl = "<your site URL";
            ClientContext clientContext = new ClientContext(siteUrl);
            //setOnlineCredential(clientContext);
            Web web = clientContext.Site.RootWeb;
            List list = web.Lists.GetByTitle("<your list name>");
            clientContext.Load(list);
            clientContext.ExecuteQuery();

            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = @"<View Scope='RecursiveAll'>
                                      <Query>
                                      </Query>
                                  </View>";

            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();

            List<ListItem> itemList = listItems.ToList<ListItem>();

            Parallel.ForEach(itemList, item => {
                if (item.FileSystemObjectType == FileSystemObjectType.File)
                {
                    Console.WriteLine("Title is: " + item["FileLeafRef"]);
                }
            });

            Console.WriteLine("finish");
            Console.ReadLine();
        }

    }
}



关于您的代码,建议您首先通过以下命令转换为列表.



About your code, I suggest you first convert to list by the following command.

List<ListItem> itemList = listItems.ToList<ListItem>()


最好的问候,
卡尔·周


Best Regards,
Carl Zhou


这篇关于Parallel.ForEach在执行共享点库文件时返回不一致的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:27