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.pswp.cn/news/259472.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/259472.shtml
英文地址,請注明出處:http://en.pswp.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; 在…