如何获取检查点记录并处理来自foreach C#的剩余数据 [英] how to take checkpoint record and process remaining data from foreach c#

查看:65
本文介绍了如何获取检查点记录并处理来自foreach C#的剩余数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I have multiple files to be process from list.

如果由于某种原因而中断了列表,则下次执行程序时,它将获取最后执行的记录并开始处理其他记录,而不是从0索引开始.

If because of any reason list is interrupted , when next time we execute program it takes the last executed records and start process the further records instead start from 0 index.

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(); 

foreach (var item in listItems)
{
    if (item.FileSystemObjectType == FileSystemObjectType.File)
    {

For example I have 10 files to process. After each file I'm saving file name into log so I have last executed filename. If because of any reason program shutdown , when we run next time it should take the next file to process instead take it from start.

我可以在这里做任何检查点的事情吗?

Any thing I can do like checkpoint here ?

SE

推荐答案

在库中添加一个标识符列,例如"Status".列,然后在foreach循环的结尾,用例如"Completed"的值更新此列.

Add a identifier column in the library for example "Status" Column and then in the end of foreach loop, update this column with a value for example "Completed".

然后在CAML中使用条件不等于"过滤列值.到该值,因此在重新启动程序时,listItems集合将始终具有尚未执行的项目,而是从已保存到日志的开始文件中删除 文件:

Then in the CAML filtered the column value with Condition "not equal" to the value, so when restart the program the listItems collection will always have the items which has not been executed instead from start file which has been saving into log file:

        static void Main(string[] args)
        {

            var clientContext = new ClientContext("http://sp/sites/dev/");

            Web web = clientContext.Web;
            clientContext.Load(web);
            clientContext.Load(web.Lists);
            clientContext.Load(web, wb => wb.ServerRelativeUrl);
            clientContext.ExecuteQuery();

            List list = web.Lists.GetByTitle("MyRecordLibrary");
            clientContext.Load(list);
            clientContext.ExecuteQuery();

            Folder folder = web.GetFolderByServerRelativeUrl(web.ServerRelativeUrl + "/MyRecordLibrary/");
            clientContext.Load(folder);
            clientContext.ExecuteQuery();

            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = @"<View Scope='RecursiveAll'><Query><Where><Neq><FieldRef Name='Statuscheck' /><Value Type='Text'>Completed</Value></Neq></Where></Query></View>";

            camlQuery.FolderServerRelativeUrl = folder.ServerRelativeUrl;
            ListItemCollection listItems = list.GetItems(camlQuery);
            clientContext.Load(listItems);
            clientContext.ExecuteQuery();
            foreach (ListItem item in listItems)
            {
                if (item.FileSystemObjectType==FileSystemObjectType.File)
                {
                    //do your logic firstly


                    //then in the end update the field value to Completed
                    clientContext.Load(item);
                    item["Statuscheck"] = "Completed";
                    item.Update();
                    clientContext.ExecuteQuery();
                }
                
            }


        }

有一个项目已经像上面一样完成保存,然后在程序重新运行时,它将自动跳过该项目并开始执行其他尚未执行的项目.

There is one item has been completed saving like above and then when program rerun, it will automatically skip this item and start for other items which has not been executed.

谢谢

最好的问候


这篇关于如何获取检查点记录并处理来自foreach C#的剩余数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆