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,一经查实,立即删除!

相关文章

Cydia源局域网化

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

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…

redis windows下的环境搭建

先说下安装吧&#xff01;感觉这东西跟mongodb差不多&#xff0c;安装和布置挺简单&#xff0c;下载地址&#xff1a;https://github.com/dmajkic/redis/downloads 下载下来的包里有两个&#xff0c;一个是32位的&#xff0c;一个是64位的。根据自己的实情情况选择&#xff0c;…

jsp乱码

自从重装系统之后电脑运行程序总是容易出现一些微妙的乱码&#xff0c;一直都没有彻底解决&#xff0c;有时候在别的机器上运行无误的代码一到我的机器上就出现一些问题。 myeclipse编码方式怎么改都无效&#xff0c;每次只能再代码中加上几行转码的语句 今天终于找到罪魁祸首-…

如何使用Notepad++格式化XML文件

经常会从数据库中读到挤在一起的XML, 整理它们的格式需要使用一些工具. 比如笔者之前使用过online的tool. 后来经同事介绍, 改用VS2008的CtrlK, CtrlF来整理. 但是VS2008有点庞大, 开启起来还是有点慢, 用起来也远不如Notepad顺手. 于是笔者Google了一把. 找到了下面的步骤, 非…

联想u盘linux安装教程,联想笔记本用U盘安装 winXP系统教程

联想笔记本用U盘安装 winXP系统教程。联想笔记本是指联想集团生产的便携手提电脑。 联想集团成立于1984年&#xff0c;由中科院计算所投资20万元人民币、11名科技人员创办&#xff0c;到今天已经发展成为一家在信息产业内多元化发展的大型企业集团。今天小编将给大家介绍使用U盘…

为什么Docker是云计算必然的现在和未来

Docker所代表的Container技术&#xff0c;是对内核的Cgroups、namespace等内容的使用.Linux Containerlxc借助BootZdocker可以实现在Mac和Windows上运行CGroups限制容器的资源使用Namespace机制&#xff0c;实现荣期间的隔离chroot,文件系统的隔离Linux内核提供的限制&#xff…

linux桌面环境 mac os,在Windows或Linux桌面上使用Mac OS Dashboard Widget | MOS86

Windows Vista中引入了Gadgets&#xff0c;并在Windows 7中继续使用。它们允许您从最新的新闻更新到月球的各个阶段查看各种信息&#xff0c;并在桌面上使用一些有用的实用程序。我们以前写过一个程序&#xff0c;允许您在Windows XP中使用Windows 7风格的小工具和一些实用程序…

spark 笔记 16: BlockManager

spark 笔记 16&#xff1a; BlockManager 先看一下原理性的文章&#xff1a;http://jerryshao.me/architecture/2013/10/08/spark-storage-module-analysis/ &#xff0c;http://jerryshao.me/architecture/2013/10/08/spark-storage-module-analysis/ , 另外&#xff0c;spar…

C++ stringstream介绍,使用方法与例子

C引入了ostringstream、istringstream、stringstream这三个类&#xff0c;要使用他们创建对象就必须包含sstream.h头文件。   istringstream类用于执行C风格的串流的输入操作。 ostringstream类用于执行C风格的串流的输出操作。 strstream类同时可以支持C风格的串流的输入…

CheckBox控件

前台代码&#xff1a; 1 <asp:CheckBox ID"CheckBox1" runat"server" Text "苹果"/> 2 <asp:CheckBox ID"CheckBox2" runat"server" Text "柠檬"/> 3 <asp:CheckBox ID"CheckBox3" runa…

go.js中的图标(icons)的使用

2019独角兽企业重金招聘Python工程师标准>>> 1、图标库下载&#xff1a; 将icons引入&#xff1a;http://gojs.net/latest/samples/icons.js 2、样式演示 地址&#xff1a;http://gojs.net/latest/samples/icons.html 转载于:https://my.oschina.net/u/2391658/blog…

Pygame - Python游戏编程入门(1)

前言 在上一篇中&#xff0c;我们初步熟悉了pygame的控制流程&#xff0c;但这对于一个游戏而言是远远不够的。所以在这一篇中&#xff0c;我们的任务是添加一架飞机&#xff08;玩家&#xff09;&#xff0c;并且能够控制它进行移动&#xff0c;这样我们就又离目标进了一步了~…

AQS浅析

2019独角兽企业重金招聘Python工程师标准>>> AQS的原理浅析 本文是《Java特种兵》的样章&#xff0c;本书即将由工业出版社出版 AQS的全称为&#xff08;AbstractQueuedSynchronizer&#xff09;&#xff0c;这个类也是在java.util.concurrent.locks下面。这个类似乎…

编程如写作

昨晚似乎是个适合写作的夜&#xff0c;不论是自己还是朋友&#xff0c;都比平常更容易被触动。看着微博上朋友们的心路&#xff0c;想写点什么却似乎找不出非常值得大书特书的主题&#xff0c;只是歪坐在电脑旁&#xff0c;喝着咖啡&#xff0c;单曲循环着仓木麻衣的《time aft…

工作环境总结(1)开发环境搭建

1、安装git 安装文件&#xff1a;Git-2.12.0-64-bit.exe 下载地址&#xff1a;https://github.com/git-for-windows/git/releases/download/v2.12.0.windows.1/Git-2.12.0-64-bit.exe 在git bash中配置&#xff0c;git bash命令行中执行&#xff08;只有使用到egit时使用&…

15款的视频处理软件免费下载

因为需要购买昂贵的视频处理软件和高性能图形计算机&#xff0c;所以视频处理是一项比较耗费金钱的技术活。正是由于这样&#xff0c;一部分人选择使用性能较好的免费在线编辑软件&#xff0c;无需太多视频处理知识便可在浏览器中剪切和编辑视频。然而&#xff0c;当我们无法连…

液位系统c语言程序,超声波自动测量物体液位系统的设计

超声波自动测量物体液位系统的设计(任务书,毕业论文15000字)摘要本系统以STC89C52单片机为核心&#xff0c;通过硬件电路连接和软件程序的编写实现通用型超声波自动测量物体液位系统的设计。其主要原理是由单片机控制超声波发射电路发射超声波&#xff0c;超声波接收电路接收遇…

android-sdk-windows版本号下载

Android SDK 4.0.3 开发环境配置及执行 近期又装了一次最新版本号的ADK环境 眼下最新版是Android SDK 4.0.3 本文的插图和文本尽管是Android2.2的 步骤都是一样的&#xff0c;假设安装的过程中遇到什么问题&#xff0c;能够留言&#xff0c;我会尽快回复&#xff01; 系统环境的…

emacs-w3m查看html帮助手册

<?xml version"1.0" encoding"utf-8"?> emacs-w3m查看html帮助手册emacs-w3m查看html帮助手册 Table of Contents 1. 使用效果2. 为什么要用emacs-w3m来查看html的帮助手册&#xff1f;3. 什么是w3m?4. 配置5. 额外资源1 使用效果 使用快捷键C-c …