private async void response(IAsyncResult res) { IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, multicastPort); byte[] received = receive.EndReceive(res, ref RemoteIpEndPoint); receive.BeginReceive(new AsyncCallback(response), null); //Process codes string device = Encoding.ASCII.GetString(received); Debug.WriteLine(device); //Example format: 192.168.1.234,ACCF239EF102,HF-LPB100-ZJ200 string[] data = device.Split(','); if (data.Length == 3) //A string we are interested in contains three parts { IPAddress iP = IPAddress.Parse(data[0]); //We only care about the first part, which is an ip //iPAddresses.Add(iP);//Yay sets! WifiLed led = new WifiLed(iP, data[1]); //New Led found if (!foundLeds.Contains(led)) { Debug.WriteLine("Found new a device at: " + data[0] + "| contained in foundLeds: " + foundLeds.Contains(led)); foundLeds.Add(led); //checkedListBoxDevices.Items.Add(led); //this.Invoke((MethodInvoker)(() => OutputBox.Items.Add(engineOutput))); //check if we have a custom name for this led string custom = xmlSettings.FindCustomName(led.macAddress); if (custom != null && custom != "") { Debug.WriteLine("[Mainview.respons] custom = {0}", custom); led.name = custom; } this.Invoke((MethodInvoker)(() => checkedListBoxDevices.Items.Add(led))); if (checkedListBoxDevices.Items.Count == 1) //first item is selected. so that we have something to work with, supress event { this.checkedListBoxDevices.SelectedValueChanged -= new EventHandler(checkedListBoxDevices_SelectedIndexChanged); this.Invoke((MethodInvoker)(() => checkedListBoxDevices.SetSelected(0, true))); this.checkedListBoxDevices.SelectedValueChanged += new EventHandler(checkedListBoxDevices_SelectedIndexChanged); } //add event listener for this led led.WifiLedUpdated += WifiLedUpdatedListener; //led.getStatus(); //Task task = new Task(led.getStatus); //await Task.Run(() => led.getStatus()); //Thread thread = new Thread(led.getStatus); //thread.Start(); //Gather information asynchrounously Task.Factory.StartNew(() => led.GetStatus()); } } }
private void checkedListBoxDevices_SelectedIndexChanged(object sender, EventArgs e) { selectedLed = (WifiLed)checkedListBoxDevices.SelectedItem; updateView(); /* * Debug.WriteLine("Index==============================="); * Debug.WriteLine("[{0}] ON = {1}",selectedLed.name, selectedLed.Active); * Debug.WriteLine("[{0}] Red = {1}", selectedLed.name, selectedLed.Red); * Debug.WriteLine("[{0}] Green = {1}", selectedLed.name, selectedLed.Green); * Debug.WriteLine("[{0}] Blue = {1}", selectedLed.name, selectedLed.Blue); * Debug.WriteLine("[{0}] IP = {1}", selectedLed.name, selectedLed.iPAddress); * Debug.WriteLine("[{0}] Name = {1}", selectedLed.name, selectedLed.name); * Debug.WriteLine("==============================="); */ }