Using Timed Action to retry ConnectWorker

Oct 19, 2012 at 11:16 PM

Hi Thomas,

I've just noticed the new version (better late than never!) and am interested in using the Timed Action to keep retrying t0 connect to a worker if the worker is not available (has not been started). I've used the same code as you've published but 1) My connect never starts 2) How do I stop the rest of the method from running (after the .Invoke()) whilst waiting for the retry (i.e. block the rest of the method till there is a connection established)

I have something like this:

 

 

var timedAction = _space.CreateTimedAction(

 

TimeSpan.FromSeconds(30),

() => {

_worker = _space.ConnectWorker<

PDispatcherWorker>(dispatcherAddress);

 

return XcoTimedAction.Continuation.Stop;

 

// OR SHOULD IT BE
 

 

 

 

 

 

 

 

 

 

 

 

 

//try

 

//{

 

// SiAuto.Main.LogMessage("Attempting to connect to dispatcher");

 

// _worker = _space.ConnectWorker<PDispatcherWorker>(dispatcherAddress);

 

// return XcoTimedAction.Continuation.Stop;

 

//}

 

//catch (System.Exception ex)

 

//{

 

// SiAuto.Main.LogMessage("Failed to connect to dispatcher. Retrying");

 

// return XcoTimedAction.Continuation.Reschedule;

 

//}

 

});

timedAction.Invoke();

 ...rest of code should run once we have a connection

 Any help gratefully received

Kind regards



Mike

Coordinator
Oct 22, 2012 at 8:24 AM

Hi Mike,

If you want to reschedule the action so often until a connection is established, then I would say that the second variant is the right one. However, I don't know if a TimedAction is really what you are looking for if you want to synchronously wait until the connection has been made. The TimedAction runs asynchronously, so if you want to wait on something you would e.g. need to use a ManualResetEvent. But if you do that, it would be easier to just make a loop with Thread.Sleep() between the connection tries, that runs synchronously in your code.

If you don't actually need to wait synchronously you could use a port to continue after the connection has been established. Something like:

var continuationPort = space.Receive<PDispatcherWorker>(w => ... do something with connected worker ...);

and within the timed action:

...
_worker = _space.ConnectWorker<PDispatcherWorker>(dispatcherAddress);
continuationPort.Post(_worker);
return XcoTimedAction.Continuation.Stop;
...

Hope I could help you!

Best Regards
Thomas