Limit simultaneously running ManagedOp?

StatusIDPriorityType
Closed24082CriticalQuestion
ProfileReply
Alex MillerClient

Hello,

Could you please provide an example on how to limit the amount of simultaneously running ManagedOperation?

Thanks!

Alex

Replies

UserDescriptionPosted On
Sasha (Xafari Support)Client

Hi Alexandre,

 

Thanks, we are already researching this issue.
It will take some additional time.

 

Feel free contact us if you need further assistance or have additional questions.
Regards, Sasha.

NikolayAgent

Hello Alex,

I'm sorry for the delayed answer.

One of the easiest ways to limit the amount of simultaneously running ManagedOperation is following.
Let assume you create an instance of ManagedOperation and set the property ProcessCode to some Action that will be executed. In a class that contains this Action you can add private static variables to count currently running operations.
See an example below.


public class SumOperation
{
    private static int _maximumOperations = 5;
    private static int _runningOperations = 0;
    private object _locker = new object();

    public SumOperation(XafApplication application)
    {
        Application = application;
    }

    public void Execute(IManagedOperation operation)
    {
        if (_runningOperations >= _maximumOperations)
            throw new InvalidOperationException("You cannot start more than 5 SumOperations.");

        try
        {
            IncRunningOperations();
            operation.NextStep("Step 1...");
            // Thread.Sleep(10000);
            // Call step 1 method

            operation.NextStep("Step 2...");
            // Thread.Sleep(10000);
            // Call step 2 method
        }
        finally
        {
            DecRunningOperations();
        }
    }

    private void IncRunningOperations()
    {
        lock (_locker)
        {
            _runningOperations++;
        }
    }

    private void DecRunningOperations()
    {
        lock (_locker)
        {
            _runningOperations--;
        }
    }
}

...
// We create and run ManagedOperation somewhere else in our code
var sumOperation = new SumOperation(Application);
var process = new ManagedOperation(Application)
{
    Name = "SumOperation",
    ProcessCode = sumOperation.Execute,
    TotalStep = 2
};
process.Start();
//
...

NikolayAgent

If you have any further question feel free to ask.

Best regards, Nikolay.

Alex Miller

Thanks Nikolay. Your example is clear and works perfectly.

Alex

× This ticket is closed.

Write US