public static bool ToStarting(ref ForwardedPortStatus status)
{
// attemp to transition from Stopped to Starting
var previousStatus = Interlocked.CompareExchange(ref status, Starting, Stopped);
if (previousStatus == Starting || previousStatus == Started)
{
// port is already Starting or Started, so no transition to Starting is necessary
return false;
}
// we've successfully transitioned from Stopped to Starting
if (status == Starting)
return true;
// there's no valid transition from status to Starting
throw new InvalidOperationException(string.Format("Forwarded port cannot transition from '{0}' to '{1}'.",
previousStatus,
Starting));
}
}