/// <summary> /// 单例模式 /// </summary> /// <returns></returns> public static MyTcpClient getInstance() { if (singleton == null) { lock (lazylock) { if (singleton == null) { singleton = new MyTcpClient(); } } } return(singleton); }
private void MainForm_Load(object sender, EventArgs e) { MyTcpClient client = MyTcpClient.getInstance(); //保证回调函数是在创建他的上下文执行(一般是UI线程) client.AsyncOperation = AsyncOperationManager.CreateOperation(null); /* * client.Error += Client_Error; ; * client.Receive += Client_Receive; ; * client.Connected += Client_Connected; */ client.Connect(); comboDeadZone.Items.AddRange(Enum.GetNames(typeof(GamePadDeadZone))); comboDeadZone.SelectedItem = reporterState.DeadZone.ToString(); pollingWorker.RunWorkerAsync(); }
private void UpdateState() { checkA.Checked = reporterState.LastActiveState.Buttons.A == XInputDotNetPure.ButtonState.Pressed; checkB.Checked = reporterState.LastActiveState.Buttons.B == XInputDotNetPure.ButtonState.Pressed; checkX.Checked = reporterState.LastActiveState.Buttons.X == XInputDotNetPure.ButtonState.Pressed; checkY.Checked = reporterState.LastActiveState.Buttons.Y == XInputDotNetPure.ButtonState.Pressed; checkStart.Checked = reporterState.LastActiveState.Buttons.Start == XInputDotNetPure.ButtonState.Pressed; checkBack.Checked = reporterState.LastActiveState.Buttons.Back == XInputDotNetPure.ButtonState.Pressed; checkGuide.Checked = reporterState.LastActiveState.Buttons.Guide == XInputDotNetPure.ButtonState.Pressed; checkStickLeft.Checked = reporterState.LastActiveState.Buttons.LeftStick == XInputDotNetPure.ButtonState.Pressed; checkStickRight.Checked = reporterState.LastActiveState.Buttons.RightStick == XInputDotNetPure.ButtonState.Pressed; checkShoulderLeft.Checked = reporterState.LastActiveState.Buttons.LeftShoulder == XInputDotNetPure.ButtonState.Pressed; checkShoulderRight.Checked = reporterState.LastActiveState.Buttons.RightShoulder == XInputDotNetPure.ButtonState.Pressed; checkDPadUp.Checked = reporterState.LastActiveState.DPad.Up == XInputDotNetPure.ButtonState.Pressed; checkDPadRight.Checked = reporterState.LastActiveState.DPad.Right == XInputDotNetPure.ButtonState.Pressed; checkDPadDown.Checked = reporterState.LastActiveState.DPad.Down == XInputDotNetPure.ButtonState.Pressed; checkDPadLeft.Checked = reporterState.LastActiveState.DPad.Left == XInputDotNetPure.ButtonState.Pressed; labelTriggerLeft.Text = FormatFloat(reporterState.LastActiveState.Triggers.Left); labelTriggerRight.Text = FormatFloat(reporterState.LastActiveState.Triggers.Right); labelStickLeftX.Text = FormatFloat(reporterState.LastActiveState.ThumbSticks.Left.X); labelStickLeftY.Text = FormatFloat(reporterState.LastActiveState.ThumbSticks.Left.Y); labelStickRightX.Text = FormatFloat(reporterState.LastActiveState.ThumbSticks.Right.X); labelStickRightY.Text = FormatFloat(reporterState.LastActiveState.ThumbSticks.Right.Y); if (reporterState.LastActiveState.Buttons.Start == XInputDotNetPure.ButtonState.Pressed) { timerStart.Start(); } else { timerStart.Stop(); } if (reporterState.LastActiveState.Buttons.Back == XInputDotNetPure.ButtonState.Pressed) { timerBack.Start(); } else { timerBack.Stop(); } for (int i = 0; i < 4; i++) { controllerControls[i].Visible = i == reporterState.LastActiveIndex && reporterState.LastActiveState.IsConnected; } PositionStickControl(stickControls[0], stickControlPositions[0], reporterState.LastActiveState.ThumbSticks.Left); PositionStickControl(stickControls[1], stickControlPositions[1], reporterState.LastActiveState.ThumbSticks.Right); //------------------------------------------ String temp = ""; if (checkA.Checked == false) { if (this.isRunning == true) { this.isRunning = false; // command stop temp = "0"; } } else { if (this.isRunning == false) { this.isRunning = true; // command go forward W1 temp = temp + "W1"; } if (checkDPadUp.Checked == true) { temp = temp + "W1"; } if (checkDPadDown.Checked == true) { // command go back Q2 temp = temp + "Q2"; } if (checkDPadLeft.Checked == true) { // command go left E4 temp = temp + "E4"; } if (checkDPadRight.Checked == true) { // command go right R3 temp = temp + "R3"; } } if (temp != "") { temp = temp + "#"; if (temp != this.command) { MyTcpClient client = MyTcpClient.getInstance(); this.command = temp; client.SendMsg(this.command); } } }