Async. BPM execution; still broken 10.2.200.3

Hi folks,

This is my first post on here, but since this is a really common and problematic issue, here is the solution I use for my asynchronous BPMs.

First, create your BPM and flag it as synchronous. Then add a code block to it, and use lambda delegates to define the code to call asynchronously, and call it with .NET’s Tasks.Parallel namespace. This always works, no ifs or buts, and doesn’t rely on a change of mood in Epicor’s code.

// Define a lambda Action<T1, T2, ...> or Func<T1, T2, ...> to be called asynchronously
Action<string> TestMethod =
(path) =>
{
    try
    {
        for (int i = 0; i < 100; i++)
        {
            System.IO.File.AppendAllText(path, i.ToString() + "\r\n"); 
        }
    }
    catch (Exception ex)
    {
        // ...
    }
};


// Call it using the Tasks.Parallel namespace. The BPM data will stay on the heap until the method has finished executing, 
// and then GC will take place. Do not use .BeginInvoke() on the action, as that requires a .EndInvoke(), and if it doesn't
// come BPM data stays on the heap indefinitely.
new System.Threading.Tasks.Task(() => TestMethod(@"C:\temp\Test.txt")).Start();
9 Likes