/// <summary> /// Initializes a new instance of the MainViewModel class /// </summary> /// <param name="displayResponder">Captures all responses from the reader and relays them to the user interface</param> /// <param name="commander">Used to setup the responder chain and execute ASCII commands</param> /// <param name="settings">The display settings</param> public MainViewModel(DisplayResponder displayResponder, IAsciiCommandExecuting commander, IDisplaySettings settings, serviceSocket socket) { InventoryCommand inventoryResponder; BarcodeCommand barcodeResponder; if (displayResponder == null) { throw new ArgumentNullException("displayResponder"); } if (commander == null) { throw new ArgumentNullException("commander"); } if (settings == null) { throw new ArgumentNullException("settings"); } this.socket = socket; this.settings = settings; // Create a display responder to capture and display all reader responses displayResponder.ReceivedLine += delegate(object sender, AsciiLineEventArgs e) { this.OnResponseLine(e.Line.FullLine); }; // setup an asynchronous responder for inventory inventoryResponder = new InventoryCommand(); inventoryResponder.TransponderReceived += this.AsynchronousTransponder_Received; // setup an asynchronous responder for barcodes barcodeResponder = new BarcodeCommand(); barcodeResponder.BarcodeReceived += this.AsynchronousBarcode_Received; // set up the responder chain this.commander = commander; this.commander.ClearResponders(); this.commander.AddResponder(new LoggerResponder()); this.commander.AddResponder(displayResponder); this.commander.AddSynchronousResponder(); this.commander.AddResponder(inventoryResponder.Responder); this.commander.AddResponder(barcodeResponder.Responder); this.synchronousBarcodeCommand = new BarcodeCommand(); this.synchronousBarcodeCommand.BarcodeReceived += this.SynchronousBarcode_Received; this.synchronousInventoryCommand = new InventoryCommand(); this.synchronousInventoryCommand.TransponderReceived += this.SynchronousTransponder_Received; }
//delegate void SetTextCallBack(string text); /// <summary> /// Initializes a new instance of the MainForm class /// </summary> public MainForm() { this.reader = Service.Instance.reader; this.viewModel = Service.Instance.MainViewModel; this.socket = Service.Instance.socket; this.InitializeComponent(); this.viewModel = Service.Instance.MainViewModel; this.viewModel.TransponderMessage += this.ViewModel_TransponderMessage; this.viewModel.BarcodeMessage += this.ViewModel_BarcodeMessage; this.viewModel.ResponseLine += this.ViewModel_ResponseLine; // TODO: this.viewModel.PropertyChanged this.connectViewModel = Service.Instance.ConnectViewModel; this.connectViewModel.PropertyChanged += this.ConnectViewModel_PropertyChanged; // Create a menu of session values this.querySessionComboBox.DataSource = Enum.GetValues(typeof(QuerySession)); // Use the description when displaying to the user this.querySessionComboBox.FormattingEnabled = true; this.querySessionComboBox.Format += delegate(object sender, ListControlConvertEventArgs e) { e.Value = ((QuerySession)e.Value).Description(); }; this.queryTargetComboBox.DataSource = Enum.GetValues(typeof(QueryTarget)); this.queryTargetComboBox.FormattingEnabled = true; this.queryTargetComboBox.Format += delegate(object sender, ListControlConvertEventArgs e) { e.Value = ((QueryTarget)e.Value).Description(); }; this.Load += this.Form_Load; /* * this.mybutton.Location = new System.Drawing.Point(90, 80); * // mytestbox.Location = PointToClient(MousePosition); * this.mybutton.Size = new Size(100, 50); //自己调整 * this.mybutton.Text = "连接中..."; * Button disConnect = new Button(); * disConnect.Location = new System.Drawing.Point(90, 150); * // mytestbox.Location = PointToClient(MousePosition); * disConnect.Size = new Size(100, 50); //自己调整 * disConnect.Text = "断开连接!"; * this.Controls.Add(mybutton); //添加,添到哪里自己调整 * this.Controls.Add(disConnect); * disConnect.Click += new EventHandler(this.disConnect); */ this.FormClosing += new FormClosingEventHandler(close); //Thread workConnect = new Thread(work); //workConnect.IsBackground = true; //workConnect.Start(); //Task t1 = new Task(work); //t1.Start(); cts = new CancellationTokenSource(); var ct = cts.Token; Task task1 = new Task(() => { work(ct); }, ct); task1.Start(); }