首页 > 解决方案 > 如何等待另一个类中的某个操作开始 c# 中的下一个操作?

问题描述

我知道你不明白问题的标题。让我告诉你整个场景。

我有一个名为处理器的类。处理器可以根据某些条件获取可通知的步骤或向其他应用程序 API 发送通知。如下所示:

Method: SendOrStartProcess(Operation operation)

Method Implementation:

If(operation.steps.any(o=>o.canProcess)){
    _service.notify(operations);   //This is fine
}
else{
    var totalSteps = await _service.getAdjustableSteps(); //It returns the adjustable steps and will invoke event which is subscribed by another class named as GetAdjustableStepsEnd. It can be invoked immediately or after some time.
    //Set totalSteps in operationsc.steps and save in database and that's it.
 

}

现在,我有另一个名为“OperationHandler”的类,它订阅了 GetAdjustableStepsEnd 事件。

public OperationHandler(IUnityContainer container)
        {
            
            _agent.GetAdjustableStepsEnd += GetAdjustableStepsEnd ;
            
        }

        public async void GetAdjustableStepsEnd (object sender, GetAdjustableStepsEndEventArgs e)
        {
            // Here i will call again the above method _processor.SendOrStartProcess(e.Operations);
        }

//现在的问题是,如果事件在一段时间后调用,那很好,因为同时我在数据库中设置了状态。但是,如果它在 getAdjustableSteps 之后调用,那么我再次调用 SendOrStartProcess 并再次发送 getAdjustableSteps,因为数据库中没有设置记录。如何克服这种情况。我无法锁定它,因为许多客户都在使用它。

标签: c#

解决方案


推荐阅读