public void WhenAddInvoke_ShouldExecuteImmediately() { var testee = new ApplicationDispatcher(); var actionTester = new ActionTester(); testee.Invoke(actionTester.TestAction); actionTester.WasCalled.Should().BeTrue(); }
public void GivenException_WhenInvoke_ShouldLogInnerException() { var testee = new ApplicationDispatcher(); var actionTester = new ActionTesterWithException(); Action act = () => testee.Invoke(actionTester.TestAction); act.ShouldThrow <Exception>().Where(e => e.Message.Contains("originExceptionTest")); }
public void AddBinding <TSource>(BindingList <TSource> source) where TSource : IMenuOption { source.ListChanged += (object sender, ListChangedEventArgs e) => ApplicationDispatcher.Invoke(() => BindingChanged(e, source)); var menuItems = source.Select(MapToItem).ToArray(); this.DropDownItems.AddRange(menuItems); }
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { var handler = PropertyChanged; if (handler == null) { return; } ApplicationDispatcher.Invoke(() => handler(this, new PropertyChangedEventArgs(propertyName)) ); }
public void GivenSimpleAction_WhenInvokedTwice_ShouldExecuteJustOnce() { var testee = new ApplicationDispatcher(); var parallelActions = (from exec in Enumerable.Range(0, 100) let at = new CountActionTester() select new { Tester = at, Action = (Action)(() => testee.Invoke(at.TestAction)) }).ToArray(); Parallel.Invoke(parallelActions.Select(a => a.Action).ToArray()); parallelActions.Any(a => a.Tester.ActionCalled != 1).Should().BeFalse(); }
public void TestActionWithArguments() { var testee = new ApplicationDispatcher(); object receivedArgument = null; testee.Invoke(x => receivedArgument = x, "test"); testee.ExecuteInvokes(); var receivedString = receivedArgument as string; receivedString.Should().Be("test"); }
//Helper method that executes the Pressed or Released event for a button on the UI thread. private void ExecuteButtonChangedEvent(ControllerButtons button, bool isPressed) { ButtonEventArgs eventArgs = new ButtonEventArgs() { AffectedButton = button }; if (isPressed && ButtonPressed != null) { ApplicationDispatcher.Invoke(delegate { ButtonPressed(this, eventArgs); }); } else if (!isPressed && ButtonReleased != null) { ApplicationDispatcher.Invoke(delegate { ButtonReleased(this, eventArgs); }); } }
public void OpenSscQuery(string queryId) { Task <ScriptItem> t = Task.Factory.StartNew(() => GetScriptItem(queryId)); t.ContinueWith(task => m_Dispatcher.Invoke(() => { QueryWindowHeader queryWindowHeader = new QueryWindowHeader(task.Result); var windowsFormsHost = new WindowsFormsHost { Child = queryWindowHeader, Height = queryWindowHeader.Height }.ToRemotedElement(); m_SsmsOperations.CreateAugmentedQueryWindow(task.Result.SqlScript, task.Result.Title, windowsFormsHost); })); }
public async Task Load() { try { this.Loading = true; var users = await _roleRepository.GetAll(); var mappedRoles = users.Select(_mapper.Map <RoleListItemDto>).ToList(); ApplicationDispatcher.Invoke(() => Roles.AddRange(mappedRoles)); } catch (Exception ex) { _messageDialog.ShowError(title: General.ErrorLoadingRolesTitle, message: ex.Message); } finally { this.Loading = false; } }
//The entry point to the thread that listens for changes in Xbox controllers attached to the computer. private void ControllerListenerThread() { //Stores the previous state of the gamepad for comparison against the new state. GamePadState lastState = GamePad.GetState(PlayerIndex.One); //A dictionary of each controller button and whether or not it has changed to pressed or released. Dictionary <ControllerButtons, bool?> buttonStates = new Dictionary <ControllerButtons, bool?>(); // ReSharper disable once NotAccessedVariable Timer disconnectTimer = null; ConnectionState connState; if (lastState.IsConnected) { connState = ConnectionState.Connected; } else { connState = ConnectionState.InitDisconnect; } //While this separate worker thread is running... while (IsListeningThreadRunning) { //Sleep for whatever listener sleep delay is so the UI thread isn't flooded with events. Thread.Sleep(_listenerSleepDelay); //Get the current gamepad state. GamePadState currentState = GamePad.GetState(PlayerIndex.One); //Detect if the controller has been disconnected or was never connected since the listening thread //was started. This can save some troubleshooting headaches. switch (connState) { case ConnectionState.Connected: //Controller is connected since the last call to get state. Make sure it is still connected. if (!currentState.IsConnected) { //Controller seems to have been disconnected. connState = ConnectionState.InitDisconnect; //Instantiate the disconnect timer. If more than 5 seconds passes since the controller was //disconnected, the ControllerDisconnected event will be fired. disconnectTimer = new Timer(delegate { //Execution here means 5 seconds have passed. If connState is still set to disconnected, //the controller wasn't found in 5 seconds. // ReSharper disable once AccessToModifiedClosure if (connState == ConnectionState.InitDisconnect && ControllerDisconnected != null) { connState = ConnectionState.FullDisconnect; ApplicationDispatcher.Invoke( delegate { ControllerDisconnected(this, EventArgs.Empty); }); } //After the timer executes, set its value back to null. disconnectTimer = null; }, null, 2000, Timeout.Infinite); } break; case ConnectionState.InitDisconnect: //The controller was disconnected since the last call to get state, and a timer thread has been //initiated. Check if the controller has been found since then. if (currentState.IsConnected) { //The controller was found again. Change connection state to connected so when the timer //finishes, it won't fire the ControllerDisconnected event. Also note that I won't fire //the ControllerReconnected event because the 5 seconds of disconnection did not pass. This //is so clients are not alerted of a disconnect that was really just a small "hiccup" in //the connection. connState = ConnectionState.Connected; } //Controller has still not been found. Sleep again. continue; case ConnectionState.FullDisconnect: //The controller was fully disconnected, and the ControllerDisconnected event was fired. Just //keep checking for reconnection infinitely. if (currentState.IsConnected) { //Controller reconnected. connState = ConnectionState.Connected; //Fire the ControllerReconnected event. if (ControllerReconnected != null) { ApplicationDispatcher.Invoke( delegate { ControllerReconnected(this, EventArgs.Empty); }); } } else { continue; //Sleep again. } break; } //Check if the actual state of the controller has changed since the last call to GetState. If it hasn't //changed, make the thread sleep again. Additionally, if the controller isn't connected, just sleep. if (currentState.PacketNumber == lastState.PacketNumber) { continue; } //Store the current pressed, released or no change values of each button in the dictionary. This helps //reduce the amount of repetitive, copy pasted code. buttonStates[ControllerButtons.A] = CompareButtonStates(lastState.Buttons.A, currentState.Buttons.A); buttonStates[ControllerButtons.B] = CompareButtonStates(lastState.Buttons.B, currentState.Buttons.B); buttonStates[ControllerButtons.X] = CompareButtonStates(lastState.Buttons.X, currentState.Buttons.X); buttonStates[ControllerButtons.Y] = CompareButtonStates(lastState.Buttons.Y, currentState.Buttons.Y); buttonStates[ControllerButtons.Start] = CompareButtonStates(lastState.Buttons.Start, currentState.Buttons.Start); buttonStates[ControllerButtons.Back] = CompareButtonStates(lastState.Buttons.Back, currentState.Buttons.Back); buttonStates[ControllerButtons.RightShoulder] = CompareButtonStates(lastState.Buttons.RightShoulder, currentState.Buttons.RightShoulder); buttonStates[ControllerButtons.LeftShoulder] = CompareButtonStates(lastState.Buttons.LeftShoulder, currentState.Buttons.LeftShoulder); buttonStates[ControllerButtons.Guide] = CompareButtonStates(lastState.Buttons.Guide, currentState.Buttons.Guide); buttonStates[ControllerButtons.RightStick] = CompareButtonStates(lastState.Buttons.RightStick, currentState.Buttons.RightStick); buttonStates[ControllerButtons.LeftStick] = CompareButtonStates(lastState.Buttons.LeftStick, currentState.Buttons.LeftStick); buttonStates[ControllerButtons.DPadUp] = CompareButtonStates(lastState.DPad.Up, currentState.DPad.Up); buttonStates[ControllerButtons.DPadDown] = CompareButtonStates(lastState.DPad.Down, currentState.DPad.Down); buttonStates[ControllerButtons.DPadLeft] = CompareButtonStates(lastState.DPad.Left, currentState.DPad.Left); buttonStates[ControllerButtons.DPadRight] = CompareButtonStates(lastState.DPad.Right, currentState.DPad.Right); //Loop through the dictionary calling the button pressed or released events as necessary for buttons. foreach (var keyValPair in buttonStates) { //A value of true for the keyValPair means the button is pressed, false means it has been released, //and null means there has been no change. if (keyValPair.Value != null) { ExecuteButtonChangedEvent(keyValPair.Key, (bool)keyValPair.Value); } } //Note that all deadzone and threshold calculations have been taken care of by the the XInput library //being used by our project. var prevLeftThumb = lastState.ThumbSticks.Left; var prevRightThumb = lastState.ThumbSticks.Right; var curLeftThumb = currentState.ThumbSticks.Left; var curRightThumb = currentState.ThumbSticks.Right; //Range change calculations from http://stackoverflow.com/questions/929103/convert-a-number-range-to-another-range-maintaining-ratio //Is the new left thumbstick value outside of the deadzone? //Has the left thumbstick moved? if (Math.Abs(curLeftThumb.X - prevLeftThumb.X) > float.Epsilon || Math.Abs(curLeftThumb.Y - prevLeftThumb.Y) > float.Epsilon) { if (ThumbstickMoved != null) { ThumbAxisEventArgs args = new ThumbAxisEventArgs(); //Change the thumbstick ranges from [-1, 1] to [-100, 100] args.XValue = (((curLeftThumb.X - ThumbstickStandardRangeMin) * Thumbstick100Range) / ThumbstickStandardRange) + Thumbstick100RangeMin; args.YValue = (((curLeftThumb.Y - ThumbstickStandardRangeMin) * Thumbstick100Range) / ThumbstickStandardRange) + Thumbstick100RangeMin; args.Side = ControllerSide.Left; ApplicationDispatcher.Invoke(delegate { ThumbstickMoved(this, args); }); } } //Has the right thumbstick moved? if (Math.Abs(curRightThumb.X - prevRightThumb.X) > float.Epsilon || Math.Abs(curRightThumb.Y - prevRightThumb.Y) > float.Epsilon) { if (ThumbstickMoved != null) { ThumbAxisEventArgs args = new ThumbAxisEventArgs(); //Change the thumbstick ranges from [-1, 1] to [-100, 100] args.XValue = (((curRightThumb.X - ThumbstickStandardRangeMin) * Thumbstick100Range) / ThumbstickStandardRange) + Thumbstick100RangeMin; args.YValue = (((curRightThumb.Y - ThumbstickStandardRangeMin) * Thumbstick100Range) / ThumbstickStandardRange) + Thumbstick100RangeMin; args.Side = ControllerSide.Right; ApplicationDispatcher.Invoke(delegate { ThumbstickMoved(this, args); }); } } //Has the left trigger changed? if (Math.Abs(currentState.Triggers.Left - lastState.Triggers.Left) > float.Epsilon) { if (TriggerMoved != null) { TriggerAxisEventArgs args = new TriggerAxisEventArgs(); //Change the trigger range from [0, 1] to [0, 100]. args.Side = ControllerSide.Left; args.ZValue = (((currentState.Triggers.Left - TriggerStandardRangeMin) * Trigger100Range) / TriggerStandardRange) + Trigger100RangeMin; ApplicationDispatcher.Invoke(delegate { TriggerMoved(this, args); }); } } //Has the right trigger changed? if (Math.Abs(currentState.Triggers.Right - lastState.Triggers.Right) > float.Epsilon) { if (TriggerMoved != null) { TriggerAxisEventArgs args = new TriggerAxisEventArgs(); //Change the trigger range from [0, 1] to [0, 100]. args.Side = ControllerSide.Right; args.ZValue = (((currentState.Triggers.Right - TriggerStandardRangeMin) * Trigger100Range) / TriggerStandardRange) + Trigger100RangeMin; ApplicationDispatcher.Invoke(delegate { TriggerMoved(this, args); }); } } lastState = currentState; } }
private static void InvokeAppDispatcherDynamic(dynamic d, object sender, dynamic e, List <Exception> exceptions) { ApplicationDispatcher.Invoke(() => InvokeDynamic(d, sender, e, exceptions)); }
private static void InvokeAppDispatcher(Delegate d, object sender, EventArgs e, List <Exception> exceptions) { ApplicationDispatcher.Invoke(() => Invoke(d, sender, e, exceptions)); }