Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1898272
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:44:42+00:00 2026-05-17T06:44:42+00:00

I have the following code which lets me execute a workflow. This could be

  • 0

I have the following code which lets me execute a workflow. This could be called repeatedly. And often is. It’s also living in a webservice, so there could be multiple calls to it at the same time. This currently works. But it’s slow, since instantiating a WorkflowRuntime each time is very slow.

How can I improve this?

public class ApprovalWorkflowRunner : IApprovalWorkflowRunner
{
    private static ILogger Logger { get; set; }
    private static IRepository Repository { get; set; }

    public ApprovalWorkflowRunner(ILogger logger, IRepository repository)
    {
        Logger = logger;
        Repository = repository;
    }

    public Request Execute(Action action)
    {
        var request = new Request();

        using (var workflowRuntime = new WorkflowRuntime())
        {
            workflowRuntime.StartRuntime();
            var waitHandle = new AutoResetEvent(false);
            workflowRuntime.WorkflowCompleted += ((sender, e) =>
                                                    {
                                                        waitHandle.Set();
                                                        request = e.OutputParameters["gRequest"] as Request;
                                                    });
            workflowRuntime.WorkflowTerminated += ((sender, e) =>
                                                    {
                                                        waitHandle.Set();
                                                        Logger.LogError(e.Exception, true, action.Serialize());
                                                    });

            var parameters = new Dictionary<string, object>
                                {
                                    {"RepositoryInstance", Repository},
                                    {"RequestID", action.RequestID.ToString()},
                                    {"ActionCode", action.ToString()}
                                };

            var instance = workflowRuntime.CreateWorkflow(typeof (ApprovalFlow), parameters);
            instance.Start();
            waitHandle.WaitOne();
        }

        return request;
    }
}

Ideally, I’d like to keep one copy of the WorkflowRuntime around. But since I’m passing other objects around in the CreateWorkflow function and WorkflowCompleted event, I don’t see how it would work.

…am I missing something simple here, there’s a good chance my brain didn’t tell my body it wasn’t showing up to work today.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T06:44:43+00:00Added an answer on May 17, 2026 at 6:44 am

    One runtime can run many workflows at the same time. As per the answer here:

    That page shows some code for a WorkflowRuntime factory that I will include below, that I believe was originally taken from the Windows Workflow Foundation Step by Step Book

    public static class WorkflowFactory
    {
        // Singleton instance of the workflow runtime
        private static WorkflowRuntime _workflowRuntime = null;
    
        // Lock (sync) object
        private static object _syncRoot = new object();
    
        /// <summary>
        /// Factory method
        /// </summary>
        /// <returns></returns>
        public static WorkflowRuntime GetWorkflowRuntime()
        {
            // Lock execution thread in case of multi-threaded
            // (concurrent) access.
            lock (_syncRoot)
            {
                // Check for startup condition
                if (null == _workflowRuntime)
                {
                    // Provide for shutdown
                    AppDomain.CurrentDomain.ProcessExit += new EventHandler(StopWorkflowRuntime);
                    AppDomain.CurrentDomain.DomainUnload += new EventHandler(StopWorkflowRuntime);
    
                    // Not started, so create instance
                    _workflowRuntime = new WorkflowRuntime();
    
                    // Start the runtime
                    _workflowRuntime.StartRuntime();
                } // if
    
                // Return singleton instance
                return _workflowRuntime;
            } // lock
        }
    
        // Shutdown method
        static void StopWorkflowRuntime(object sender, EventArgs e)
        {
            if (_workflowRuntime != null)
            {
                if (_workflowRuntime.IsStarted)
                {
                    try
                    {
                        // Stop the runtime
                        _workflowRuntime.StopRuntime();
                    }
                    catch (ObjectDisposedException)
                    {
                        // Already disposed of, so ignore...
                    } // catch
                } // if
            } // if
        }
    }
    

    You would simply call

    WorkflowFactory.GetWorkflowRuntime();
    

    EDIT:
    OK sorry. You can try checking the instance is the one you expect, and returning if it’s not. Please note this code is untested, just trying to get the idea across.

    public class ApprovalWorkflowRunner : IApprovalWorkflowRunner
    {
        private static ILogger Logger { get; set; }
        private static IRepository Repository { get; set; }
    
        public ApprovalWorkflowRunner(ILogger logger, IRepository repository)
        {
            Logger = logger;
            Repository = repository;
        }
    
        public Request Execute(Action action)
        {
            var request = new Request();
    
            var workflowRuntime = WorkflowFactory.GetWorkflowRuntime();
    
            workflowRuntime.StartRuntime();
            var waitHandle = new AutoResetEvent(false);
            WorkflowInstance instance = null;
            workflowRuntime.WorkflowCompleted += ((sender, e) =>
                                                    {
                                                        if (e.WorkflowInstance != instance) return;
                                                        waitHandle.Set();
                                                        request = e.OutputParameters["gRequest"] as Request;
                                                    });
            workflowRuntime.WorkflowTerminated += ((sender, e) =>
                                                    {
                                                        if (e.WorkflowInstance != instance) return;
                                                        waitHandle.Set();
                                                        Logger.LogError(e.Exception, true, action.Serialize());
                                                    });
    
            var parameters = new Dictionary<string, object>
                                {
                                    {"RepositoryInstance", Repository},
                                    {"RequestID", action.RequestID.ToString()},
                                    {"ActionCode", action.ToString()}
                                };
    
            instance = workflowRuntime.CreateWorkflow(typeof (ApprovalFlow), parameters);
            instance.Start();
            waitHandle.WaitOne();
    
            return request;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use opengl in C#. I have following code which fails with
I have following Code Block Which I tried to optimize in the Optimized section
I have the following code which works just fine when the method is POST,
I have the following code which works fine. However, I only want to return
Okay so I have the following Code which appends a string to another in
I have the following JQuery code which does similar functionality like Stackoverflow where the
I have the following piece of code which replaces template markers such as %POST_TITLE%
I have the following JavaScript code: Link In which the function makewindows does not
I have the following code, which will not work. The javascript gives no errors
I have the following code, which splits up a Vector into a string vector

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.