Skip to content

Commit 46c6614

Browse files
reduced business process sleep functionality down to a single thread system instead of a thread per Sleep call.
1 parent 5f28037 commit 46c6614

File tree

5 files changed

+706
-44
lines changed

5 files changed

+706
-44
lines changed

BpmEngine.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
<TargetFrameworks>netstandard2.0;net452;net20</TargetFrameworks>
55
<RootNamespace>Org.Reddragonit.BpmEngine</RootNamespace>
66
<PackageId>Org.Reddragonit.BpmEngine</PackageId>
7-
<Version>1.9.8</Version>
7+
<Version>1.9.9</Version>
88
<Authors>Roger Castaldo</Authors>
99
<Description>A BPMN Engine written in .net. The engine attempts to read in a bpmn notation xml document defining both the process(s) as well as the diagrams. From here you can then load/unload the state, render the diagram in its current state or animated into a gif. Using the delegates for a process, you intercept and handle task and condition checking by reading additional xml held within flow and task objects.</Description>
1010
<PackageProjectUrl>https://github.com/roger-castaldo/BPMEngine</PackageProjectUrl>
1111
<PackageLicenseUrl>https://www.gnu.org/licenses/gpl-3.0.en.html</PackageLicenseUrl>
1212
<RepositoryUrl>https://github.com/roger-castaldo/BPMEngine</RepositoryUrl>
1313
<PackageTags>BPMN</PackageTags>
14-
<PackageReleaseNotes>updated package to loosen up loading restrictions by not checking for xml prefix in valid documents</PackageReleaseNotes>
15-
<AssemblyVersion>1.9.8.0</AssemblyVersion>
16-
<FileVersion>1.9.8.0</FileVersion>
14+
<PackageReleaseNotes>updated package to use a single control thread for the time delays in all business processes instead of multiple threads per process</PackageReleaseNotes>
15+
<AssemblyVersion>1.9.9.0</AssemblyVersion>
16+
<FileVersion>1.9.9.0</FileVersion>
1717
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1818
</PropertyGroup>
1919

BusinessProcess.cs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ namespace Org.Reddragonit.BpmEngine
2626
/// the suspended state loaded and resumed. It can also be cloned, including the current state and delegates in order to have more than once instance
2727
/// of the given process executing.
2828
/// </summary>
29-
public sealed class BusinessProcess
29+
public sealed class BusinessProcess : IDisposable
3030
{
3131
private static readonly TimeSpan _ANIMATION_DELAY = new TimeSpan(0,0,1);
3232
private const int _DEFAULT_PADDING = 100;
3333
private const int _VARIABLE_NAME_WIDTH = 200;
3434
private const int _VARIABLE_VALUE_WIDTH = 300;
3535
private const int _VARIABLE_IMAGE_WIDTH = _VARIABLE_NAME_WIDTH+_VARIABLE_VALUE_WIDTH;
3636

37+
private Guid _id;
3738
private bool _isSuspended = false;
3839
private ManualResetEvent _mreSuspend;
3940
private List<object> _components;
@@ -470,6 +471,7 @@ public void ErrorManualTask(string taskID, Exception ex)
470471
#endregion
471472

472473
private BusinessProcess() {
474+
_id = Utility.NextRandomGuid();
473475
_processLock = new ManualResetEvent(false);
474476
_mreSuspend = new ManualResetEvent(false);
475477
}
@@ -541,6 +543,7 @@ public BusinessProcess(XmlDocument doc, LogLevels stateLogLevel, sProcessRuntime
541543
/// <param name="logLine">The LogLine delegate</param>
542544
public BusinessProcess(XmlDocument doc, LogLevels stateLogLevel,sProcessRuntimeConstant[] constants,LogLine logLine)
543545
{
546+
_id = Utility.NextRandomGuid();
544547
_stateLogLevel = stateLogLevel;
545548
_constants = constants;
546549
_logLine = logLine;
@@ -676,8 +679,10 @@ public void Resume()
676679
}
677680
foreach (sStepSuspension ss in _state.SuspendedSteps)
678681
{
679-
Thread th = new Thread(new ParameterizedThreadStart(_suspendEvent));
680-
th.Start((object)(new object[] { ss.id, ss.EndTime }));
682+
if (DateTime.Now.Ticks < ss.EndTime.Ticks)
683+
Utility.Sleep(ss.EndTime.Subtract(DateTime.Now), this, (AEvent)_GetElement(ss.id));
684+
else
685+
CompleteTimedEvent((AEvent)_GetElement(ss.id));
681686
}
682687
WriteLogLine(LogLevels.Info, new StackFrame(1, true), DateTime.Now, "Business Process Resume Complete");
683688
}
@@ -689,24 +694,6 @@ public void Resume()
689694
}
690695
}
691696

692-
private void _suspendEvent(object parameters)
693-
{
694-
_current = this;
695-
string id = (string)((object[])parameters)[0];
696-
DateTime release = (DateTime)((object[])parameters)[1];
697-
TimeSpan ts = release.Subtract(DateTime.Now);
698-
if (ts.TotalMilliseconds > 0)
699-
Utility.Sleep(ts);
700-
IElement elem = _GetElement(id);
701-
if (elem != null)
702-
{
703-
AEvent evnt = (AEvent)elem;
704-
lock (_state) { _state.Path.SucceedEvent(evnt); }
705-
if (_onEventCompleted != null)
706-
_onEventCompleted(evnt, new ReadOnlyProcessVariablesContainer(evnt.id, _state, this));
707-
}
708-
}
709-
710697
/// <summary>
711698
/// Called to render a PNG image of the process at its current state
712699
/// </summary>
@@ -1214,8 +1201,11 @@ private void _ProcessElement(string sourceID,IElement elem)
12141201
_state.SuspendStep(evnt.id, ts.Value);
12151202
}
12161203
if (ts.Value.TotalMilliseconds > 0)
1217-
Utility.Sleep(ts.Value);
1218-
success = true;
1204+
{
1205+
Utility.Sleep(ts.Value, this, evnt);
1206+
return;
1207+
}else
1208+
success = true;
12191209
}
12201210
}else if (_isEventStartValid != null && (evnt is IntermediateCatchEvent || evnt is StartEvent))
12211211
{
@@ -1341,6 +1331,13 @@ private void _ProcessElement(string sourceID,IElement elem)
13411331
}
13421332
}
13431333

1334+
internal void CompleteTimedEvent(AEvent evnt)
1335+
{
1336+
lock (_state) { _state.Path.SucceedEvent(evnt); }
1337+
if (_onEventCompleted != null)
1338+
_onEventCompleted(evnt, new ReadOnlyProcessVariablesContainer(evnt.id, _state, this));
1339+
}
1340+
13441341
private void _MergeVariables(UserTask task, ProcessVariablesContainer variables, string completedByID)
13451342
{
13461343
_MergeVariables((ATask)task, variables, completedByID);
@@ -1450,5 +1447,17 @@ internal void WriteLogException(StackFrame sf, DateTime timestamp, Exception exc
14501447
_logException.Invoke(sf.GetMethod().DeclaringType.Assembly.GetName(), sf.GetFileName(), sf.GetFileLineNumber(), timestamp, exception);
14511448
}
14521449
#endregion
1450+
1451+
public void Dispose()
1452+
{
1453+
Utility.UnloadProcess(this);
1454+
}
1455+
1456+
public override bool Equals(object obj)
1457+
{
1458+
if (obj is BusinessProcess)
1459+
return ((BusinessProcess)obj)._id == _id;
1460+
return false;
1461+
}
14531462
}
14541463
}

0 commit comments

Comments
 (0)