This task is currently locked by a running workflow and cannot be edited

转自:http://geek.hubkey.com/2007/09/locked-workflow.html

转自:http://blogs.code-counsel.net/Wouter/Lists/Posts/Post.aspx?List=c04a88a9%2Dd138%2D4ac3%2Da2bb%2Db95c9fdd114e&ID=118

SPWorkflow.AlertTask()的时候出现“This task is currently locked by a running workflow and cannot be edited”错误

A not so uncommon error that people encounter with SharePoint workflow is running into locked tasks. You know, the error that you get when working with workflow tasks, usually on your developer machine. You might get the error message "This task is currently locked by a running workflow and cannot be edited.".

Why do I know it is not uncommon? Well, if you use my common-o-meter you'll see for yourself:

That is a lot of hits!

So many people have this issue with task locking. Generally it goes like this. A developer creates a workflow that issues a couple of tasks. Next the developer fiddles with his code to get it right. Then, returning to his test workflow, he finds that when editing a task and clicking Ok, the SharePoint UI informs him of the fact that the task is locked and cannot be edited.

So, what is going on here? There are a number of items that show up searches for this error message, but never a good explanation on why you got there in the first place. More symptom management that bug fixing! Well, the first thing to note is that the error is absolutely correct. The task is locked, but why is it locked, and was it not unlocked appropriately? Despite what many people think this has nothing to do with your DLL versions, at least not as directly as you might find written and guessed at. Here's the story.

How workflow tasks are locked

The first thing to realize is that when SharePoint workflows alter tasks there needs to be some sort of locking behavior on tasks so that you will not accidentally create race conditions and update a task simultaneously, the one update overwriting the other. Typically database level locks are used but for SharePoint Workflow tasks however a more simple, business-layer type lock suffices. Since SharePoint workflow is about humans and not about maximum near real-time performance the chance of collisions is low enough not to be worried about this. The workflow runtime in SharePoint locks tasks by setting a field and persisting that to the database. It then checks on the field value to determine whether it is locked. You can actually see the code that does this. The SPWinOEItemEventReceiver implements the ItemUpdating and ItemUpdated events. In the ItemUpdating you can find code similar to the following pseudo code

if WorkflowVersion for item not equal to 1
throw locked error
else
Place lock (Set WorkflowVersion on item to value not equal to 1)

How the lock placement is actually implemented is that the WorkflowVersion is set to the value in _UIVersion, which contains a value indicating the major / minor version of the task. Why _UIVersion? It ties the lock to a specific version of the list item, and versioning is enabled on workflow task lists. This probably allows the locks to be bypassed by other code inside SharePoint depending on the version. (By the way: do *not* use this knowledge of internals in production code)

The next interesting question is where the lock is released. The ItemUpdated event facilitates this. When the task lock is detected in the ItemUpdated event it is routed to the SPWorkflowManager. This manager runs code to dehydrate and startup the workflow (which was persisted to the database while waiting for the task change to occur). The SPWorkflowManager uses the SPWinOeHostServices workflow service to unlock the task in the PostWorkItemDequeue method and runs the workflow.

Running into the locking issue

So, the issue is that the lock is still there even though it should have been released in the ItemUpdated event. Clearly, the ItemUpdated event is where the issue lies, and like all bugs in life, you did it, and not the framework! (hope that does not come as a shocker to you) There is only one aspect of the locking that you can control, and that is the persistence and hydration of your workflow to and from the database. This is exactly what is causing the bugs. When the ItemUpdated event fires and tries to de-serialize your workflow there might be an exception during the hydration of your workflow object. This error is difficult to see since it is happening in non-user code based on an asynchronous event. When that error occurs, the task unlocking code does not run!

The general flow of events to create this issue goes something like this.

  • Developer designs a workflow which creates a task.
  • Developer tests the workflow, and runs it up to the task change activity, meaning that the workflow is now serialized in the database waiting for a task change to occur.
  • Developer spots a bug, and updates the workflow in such a way that de-serialization breaks.
  • Developer updates the task through the browser to continue the workflow .
  • Runtime bumps into the de-serialization error, and cannot continue, hence the task unlocking code does not run, and the task is locked for all eternity.

A common de-serialization issue that you might create is a change in the activity structure, or the addition of control fields in your main workflow class. 

Preventing the locking issue

Now that we have a clear understanding of the issue, there are many things you can about it. On development I'd go for re-running the entire workflow (at least when it is not too big).

On Production, it is even easier:

DO NOT UPGRADE UNTIL ALL RUNNING WORKFLOWS ARE COMPLETE

You should quiesce a workflow and when all running workflows have completed, update. Or, when you need to have the business logic available during the quiescing, you can only create a new workflow.

Hope it helps!

Posted at 11:50 AM by Wouter van Vugt | Permalink | Email this Post | Comments (0)

Locked Workflow

I've periodically come across this SPException: "This task is currently locked by a running workflow and cannot be edited" when using the SPWorkflowTask.AlterTask method, even when it seems that the workflow is not in fact locked, and is instead patiently listening for an OnTaskChangedEvent. It turns out that this exception is thrown when the WorkflowVersion of the task list item is not equal to 1, which, if you believe the error message is the same thing as checking to see if the workflow is locked. Only it isn't - apparently sometimes at least, the Workflow version is non zero and the workflow is not locked (the InternalState flag of the workflow does not include the Locked flag bits). I'm not sure why this is occurring - maybe the error message is misleading - but the following code demonstrates a dodgy sort of a workaround that I've found useful. I've no idea if this is a good idea or not, so please treat with skepticism...

代码

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using System.Collections;
using System.Threading;
namespace DevHoleDemo
{
public class WorkflowTask
    {
public static bool AlterTask(SPListItem task, Hashtable htData, bool fSynchronous, int attempts, int millisecondsTimeout)
        {
if ((int)task[SPBuiltInFieldId.WorkflowVersion] != 1)
            {
                SPList parentList = task.ParentList.ParentWeb.Lists[new Guid(task[SPBuiltInFieldId.WorkflowListId].ToString())];
                SPListItem parentItem = parentList.Items.GetItemById((int)task[SPBuiltInFieldId.WorkflowItemId]);
for (int i = 0; i < attempts; i++)
                {
                    SPWorkflow workflow = parentItem.Workflows[new Guid(task[SPBuiltInFieldId.WorkflowInstanceID].ToString())];
if (!workflow.IsLocked)
                    {
                        task[SPBuiltInFieldId.WorkflowVersion] = 1;
                        task.SystemUpdate();
break;
                    }
if (i != attempts - 1)
                        Thread.Sleep(millisecondsTimeout);
                }
            }
return SPWorkflowTask.AlterTask(task, htData, fSynchronous);
        }
    }
}

 

Anonymous said...

Thank you so much for the tip, your code did the trick. I think the reason WorkflowVersion changes is because there's a different version of the workflow dll. So when you recompile your workflow and DLL is changed in the GAC while there are still running workflows, trying to finish any of those workflows might give you "This task is currently locked.." error.

转载于:https://www.cnblogs.com/frankzye/p/3257153.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/259472.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ETL模型设计

传统的关系数据库一般采用二维数表的形式来表示数据&#xff0c;一个维是行&#xff0c;另一个维是列&#xff0c;行和列的交叉处就是数据元素。关系数据的基础是关系数据库模型&#xff0c;通过标准的SQL语言来加以实现。 数据仓库是多维数据库&#xff0c;它扩展了关系数据库…

《剑指offer》-整数中1出现的次数

题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数&#xff1f;为此他特别数了一下1~13中包含1的数字有1、10、11、12、13因此共出现6次,但是对于后面问题他就没辙了。ACMer希望你们帮帮他,并把问题更加普遍化,可以很快的求出任意非负整数区间中1出现的…

This is Me!——回顾第一个项目的前前后后

今天终于把论文敲完了&#xff0c;一路走来&#xff0c;颇多感想。遂写下以下诸多文字&#xff0c;以飨读者。 在说这个项目之前&#xff0c;先简单介绍一下我的经历。我叫王财勇&#xff0c;家是山西的&#xff0c;2009年至2013年在新疆大学就读数学专业&#xff0c;也许有人…

从零开始学JavaScript三(变量)

一、变量 ECMAscript变量是松散型变量&#xff0c;所谓松散型变量&#xff0c;就是变量名称可以保存任何类型的数据&#xff0c;每个变量仅仅是一个用于保存值的占位符。 定义变量时要使用var操作符 如&#xff1a; var message; /*定义一个名为message的变量&#xff0c;该变量…

DES加密过程例解

DES加密算法是最被广泛使用的对称加密算法&#xff0c;通过示例来演示DES、TribleDES&#xff08;3Key&#xff09; DES-ECB&#xff1a; 按8字节为单位进行加密&#xff0c;不足8字节补0key&#xff1a; 1111111111111111indata: 2222222222222222 OutData: 950973182317F8…

linux在双系统中消失了,双系统重新安装windows后,ubuntu选项消失

1、首先用LiveCD进入ubuntu2、打开终端&#xff0c;&#xff0c;输入&#xff1a;fdisk -l 查看自己linux的分区情况&#xff0c;我的分了4个区&#xff0c;swap&#xff0c;boot&#xff0c;/&#xff0c;home&#xff0c;对应的分别是&#xff1a;/dev/sda9 swap…

Cydia源局域网化

2019独角兽企业重金招聘Python工程师标准>>> 步骤 在网址根目录创建文件夹cydia&#xff0c;把你的deb文件放到 cydia/debs/ 文件夹下。在终端cd进入cydia文件夹输入命令&#xff1a;dpkg-scanpackages debs /dev/null > Packages输入命令&#xff1a;tar zcvf P…

前缀++ 后缀++ 运算符重载

下面例子程序中 const Fraction operator (int) 中 int不过是个哑元&#xff08;dummy&#xff09;,是永远用不上的 它只是用来判断&#xff0b;&#xff0b;是prefix 还是 postfix 记住&#xff0c;如果有哑元&#xff0c;则是postfix,否则&#xff0c…

固定资产调整对资产折旧的影响

固定资产折旧计提方法 一、原值增加&#xff1a; 1、已摊销资产&#xff1a; 摊销调整时间设在当期&#xff1a;(1078135) 在进行原值增加后&#xff0c;摊销日期不变时&#xff0c;折旧在当月体现。 每月新增月折旧调增金额*(1-残值率)/(折旧年限*12-已提折旧月份的个数) 例&a…

linux系统中 库分为静态库和,Linux系统静态库与共享库

8种机械键盘轴体对比本人程序员&#xff0c;要买一个写代码的键盘&#xff0c;请问红轴和茶轴怎么选&#xff1f;This article mainly introduces the statics library and shared library on Linux and has done some experiments for better comprehension.Static library&am…

软件工程概论作业01

软件工程作业01 写一个能自动生成三十道小学四则运算题目的 “软件”&#xff0c;要求&#xff1a;除了整数以外&#xff0c;还要支持真分数的四则运算&#xff08;需要验证结果的正确性&#xff09;、题目避免重复、可定制出题的数量。 思路&#xff1a;随机生成两个数进行计算…

成员指针运算符 .* 和 -*

转载&#xff1a; http://www.groad.net/bbs/thread-5548-1-1.html 有一种特殊的指针叫做成员指针&#xff0c;它们通常指向一个类的成员&#xff0c;而不是对象中成员的特定实例。 成员指针并不是真正的指针&#xff0c;它只是成员在对象中的偏移量&#xff0c;它们分别是&am…

捕捉Entity framework 6的详细异常提示

采用 try{}catch (Exception e){throw;}不能捕捉到详细异常提示, e.message的内容为"Validation failed for one or more entities. See EntityValidationErrors property for more details." 如果需要获取详细的异常提示,采用 1 try2 {3 return…

8.16——熟悉安装linux系统

一、linux的版本——CentOS CentOS&#xff08;Community ENTerprise Operating System&#xff09;是Linux发行版之一&#xff0c;它是来自于Red Hat Enterprise Linux依照开放源代码规定释出的源代码所编译而成。由于出自同样的源代码&#xff0c;因此有些要求高度稳定性的服…

linux中设置默认权限的命令,Linux默认权限掩码

Linux教程Linux教程&#xff1a;http://www.fdlly.com/m/linux文章目录默认权限掩码设置权限掩码以文字的方式设置权限掩码查看系统当前的权限掩码默认权限掩码当我们创建文件或目录时&#xff0c;系统会自动根据权限掩码来生成预设权限&#xff1b;默认情况下的umask值是022(可…

percona-toolkit工具包安装

percona-toolkit工具包同percona-xtrabackup一样都是用Perl写的工具包&#xff0c;percona-toolkit工具包是一组高级的管理mysql的工具包集&#xff0c;可以用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务&#xff0c;在生产环境中能极大的提高效率&#xff0c;安装…

C++允许重载的运算符和不允许重载的运算符

C中绝大部分的运算符允许重载&#xff0c;具体规定见表10.1。 表10.1 C允许重载的运算符双目算术运算符 (加)&#xff0c;-(减)&#xff0c;*(乘)&#xff0c;/(除)&#xff0c;% (取模) 关系运算符 (等于)&#xff0c;! (不等于)&#xff0c;< (小于)&#xff0c;> (大…

Google Mesa概览

Google Mesa的文章&#xff1a;https://research.google.com/pubs/pub42851.html https://gigaom.com/2014/08/07/google-shows-off-mesa-a-super-fast-data-warehouse-that-runs-across-data-centers/ 为什么未来的Hadoop是实时的&#xff1a; https://gigaom.com/2013/03/0…

C++数组参数应用方式探讨(转)

对于经验丰富的编程人员来说&#xff0c;C编程语言应该是他们经常使用于程序开发的一种实用性语言。那么&#xff0c;在C中&#xff0c;C数组参数永远不会按值传递。它是传递第一个元素&#xff08;准确地说是第0个&#xff09;的指针。 例如&#xff0c;如下声明&#xff1a; …

一篇关于兼容问题的基础总结

1.添加兼容文件(以 es5-shim 为例) 方法一&#xff1a; <script src"https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.7/es5-shim.min.js"></script>在你的开发中&#xff0c;在需要为他做兼容的文件引入改文件 方法二(以模块引入)&#xff1a; 在…