public void Run() { #if NET_4_6 || NET_STANDARD_2_0 runFlag.Wait(); runFlag.Reset(); #else runFlag.WaitOne(); #endif while (!terminate) { try { List <Agent> agents = simulator.GetAgents(); if (task == 0) { for (int i = start; i < end; i++) { agents[i].CalculateNeighbours(); agents[i].CalculateVelocity(context); } } else if (task == 1) { for (int i = start; i < end; i++) { agents[i].BufferSwitch(); } } else if (task == 2) { simulator.BuildQuadtree(); } else { Debug.LogError("Invalid Task Number: " + task); throw new System.Exception("Invalid Task Number: " + task); } } catch (System.Exception e) { Debug.LogError(e); } waitFlag.Set(); #if NET_4_6 || NET_STANDARD_2_0 runFlag.Wait(); runFlag.Reset(); #else runFlag.WaitOne(); #endif } }
/// <summary> /// 하나의 작업이 끝나기를 기다린다. /// </summary> /// <returns></returns> public bool WaitOne() { if (IsDebugEnabled) { log.Debug("하나의 작업이 끝나기를 기다립니다..."); } #if !SILVERLIGHT _doneWaitingEvent.Wait(); return(true); #else return(_doneWaitingEvent.WaitOne()); #endif }
public static Boolean PerformPassivation(ref Int32 transitionState, ref InProgressTracker passivationInProgress, InProgressTracker activationInProgress, Int32 waitTime, Action passivationAction) { Int32 initialState = (Int32)(new InProgressTracker().Equals(activationInProgress) ? ActivationState.DURING_ACTIVATION : ActivationState.ACTIVE); Int32 prevState; Boolean actionInvoked; Boolean tryAgain; #if SILVERLIGHT using (var waitEvt = new ManualResetEvent(false)) #else using (var waitEvt = new ManualResetEventSlim(false)) #endif { do { prevState = ApplicationSkeleton.ThreadsafeStateTransition(ref transitionState, initialState, (Int32)ActivationState.DURING_PASSIVATION, (Int32)ActivationState.PASSIVE, false, ref passivationInProgress, waitTime, passivationAction); actionInvoked = prevState == initialState; tryAgain = !actionInvoked && prevState != (Int32)ActivationState.PASSIVE && prevState != (Int32)ActivationState.DURING_PASSIVATION; if (tryAgain && initialState == (Int32)ActivationState.ACTIVE) { // Wait if we are not inside activation action, // and if transition state change failed because activation is in progress. #if SILVERLIGHT waitEvt.WaitOne(waitTime); #else waitEvt.Wait(waitTime); #endif } } while (tryAgain); } return(actionInvoked); }
public void ForceSync() { if (this.m_queueService.IsBusy || ApplicationContext.Current.GetService <ISynchronizationService>().IsSynchronizing || s_isDownloading) { throw new InvalidOperationException(Strings.err_already_syncrhonizing); } else { ManualResetEventSlim waitHandle = new ManualResetEventSlim(false); ApplicationContext.Current.SetProgress(Strings.locale_waitForOutbound, 0.1f); // Wait for outbound queue to finish EventHandler <QueueExhaustedEventArgs> exhaustCallback = (o, e) => { if (e.Queue == "outbound") { waitHandle.Set(); } }; this.m_queueService.QueueExhausted += exhaustCallback; this.m_queueService.ExhaustOutboundQueues(); waitHandle.WaitOne(); this.m_queueService.QueueExhausted -= exhaustCallback; s_isDownloading = true; try { ApplicationContext.Current.SetProgress(String.Format(Strings.locale_downloading, ""), 0); var targets = ApplicationContext.Current.Configuration.GetSection <SynchronizationConfigurationSection>().SynchronizationResources.Where(o => o.Triggers.HasFlag(SynchronizationPullTriggerType.Always) || o.Triggers.HasFlag(SynchronizationPullTriggerType.OnNetworkChange) || o.Triggers.HasFlag(SynchronizationPullTriggerType.PeriodicPoll)).ToList(); for (var i = 0; i < targets.Count(); i++) { var itm = targets[i]; ApplicationContext.Current.SetProgress(String.Format(Strings.locale_downloading, itm.ResourceType.Name), (float)i / targets.Count); if (itm.Filters.Count > 0) { foreach (var f in itm.Filters) { ApplicationContext.Current.GetService <RemoteSynchronizationService>().Pull(itm.ResourceType, NameValueCollection.ParseQueryString(f), itm.Always, itm.Name); } } else { ApplicationContext.Current.GetService <ISynchronizationService>().Pull(itm.ResourceType); } } } finally { s_isDownloading = false; } } }
public void WaitOne() { #if NET_4_6 || NET_STANDARD_2_0 if (!terminate) { waitFlag.Wait(); } #else if (!terminate) { waitFlag.WaitOne(); } #endif }
public static Int32 ThreadsafeStateTransition(ref Int32 transitionState, Int32 initialState, Int32 intermediate, Int32 final, Boolean rollbackStateOnFailure, ref InProgressTracker inProgressTracker, Int32 waitTime, Action transitionAction) { Int32 oldValue = Interlocked.CompareExchange(ref transitionState, intermediate, initialState); if (initialState == oldValue) { Boolean finished = false; try { Interlocked.Exchange(ref inProgressTracker, new InProgressTracker()); transitionAction(); finished = true; } finally { Interlocked.Exchange(ref inProgressTracker, null); Interlocked.Exchange(ref transitionState, finished || !rollbackStateOnFailure ? final : initialState); } } else if (final != oldValue && !(new InProgressTracker()).Equals(inProgressTracker)) { // We are entering mid-transition from another thread #if SILVERLIGHT using (var evt = new ManualResetEvent(false)) #else using (var evt = new ManualResetEventSlim(false)) #endif { while (inProgressTracker != null) { // Wait #if SILVERLIGHT evt.WaitOne(waitTime); #else evt.Wait(waitTime); #endif } } } return(oldValue); }
public static Boolean PerformActivation(ref Int32 transitionState, ref InProgressTracker activationInProgress, InProgressTracker passivationInProgress, Int32 waitTime, Boolean throwIfActivatingWithinPassivation, Action activationAction) { var passivationInProgressBool = new InProgressTracker().Equals(passivationInProgress); if (throwIfActivatingWithinPassivation && passivationInProgressBool) { // Trying to activate within passivation => not possible throw new InvalidOperationException("Can not activate from within passivation."); } Boolean result; Int32 prevState; Boolean tryAgain; #if SILVERLIGHT using (var waitEvt = new ManualResetEvent(false)) #else using (var waitEvt = new ManualResetEventSlim(false)) #endif { do { prevState = ThreadsafeStateTransition(ref transitionState, (Int32)ActivationState.PASSIVE, (Int32)ActivationState.DURING_ACTIVATION, (Int32)ActivationState.ACTIVE, true, ref activationInProgress, waitTime, activationAction); result = prevState == (Int32)ActivationState.PASSIVE; // Try again only if we are waiting for passivation to end in another thread tryAgain = !result && prevState == (Int32)ActivationState.DURING_PASSIVATION && !passivationInProgressBool; if (tryAgain) { #if SILVERLIGHT waitEvt.WaitOne(waitTime); #else waitEvt.Wait(waitTime); #endif } } while (tryAgain); } return(result); }