I have a service that receives requests and processes them gradually (using ActionBlock).
Sometimes I need to wait until it's processed. I have the option to enter a callback. Is there a way to pause the run until a callback is called?
Something like this, but better:
bool complete = true;
service.AddRequest(new ServiceRequest()
{
OnComplete = (req) =>
{
complete = true;
}
});
while (!complete) Thread.Sleep(100);
Longer code for context:
public class Service
{
protected ActionBlock<ServiceRequest> Requests;
public Service()
{
Requests = new ActionBlock<ServiceRequest>(req => {
// do noting
Thread.Sleep(1000);
req.OnComplete?.Invoke(req);
});
}
public void AddRequest(ServiceRequest serviceRequest) => Requests.Post(serviceRequest);
}
public class ServiceRequest
{
public Action<ServiceRequest> OnComplete;
string Foo;
}
public class App
{
public static void Start()
{
var service = new Service();
service.AddRequest(new ServiceRequest());
// I need to wait for processing
// smelly code!!
bool complete = true;
service.AddRequest(new ServiceRequest()
{
OnComplete = (req) =>
{
complete = true;
}
});
while (!complete) Thread.Sleep(100);
// smelly code!! end
}
}