public Form1() { InitializeComponent(); listBox_Logger.Items.Add("Hello World"); Robot = new RoboComm(); // 1. Synchronous Delegates Robot.dataReceivedDelegate += ProcessDataFromBot; // Anonymous delegate version Robot.dataReceivedDelegate += delegate(byte[] data){ this.Invoke((MethodInvoker)delegate { listBox_Logger.Items.Add(DateTime.Now.ToString() + " And some Anon bytes"); listBox_Logger.SelectedIndex = listBox_Logger.Items.Count - 1; }); }; // 2. Asynchronous delegate using begin/end invoke (obsolete) Robot.asyncDataProcessDelegate += ProcessDataFromBotAsync; // 3. Asynchronous delegate using TPL and await keyword Robot.asyncTplDataReceivedDelegate = async (byte[] data) => { this.Invoke((MethodInvoker)delegate { listBox_Logger_TPL.Items.Add(DateTime.Now.ToString() + " Some async/TPL bytes" ); listBox_Logger_TPL.SelectedIndex = listBox_Logger_TPL.Items.Count - 1; }); return (true); }; StartParentThread(); }
public Form1() { InitializeComponent(); listBox_Logger.Items.Add("Hello World"); Robot = new RoboComm(); // 1. Synchronous Delegates Robot.dataReceivedDelegate += ProcessDataFromBot; // Anonymous delegate version Robot.dataReceivedDelegate += delegate(byte[] data){ this.Invoke((MethodInvoker) delegate { listBox_Logger.Items.Add(DateTime.Now.ToString() + " And some Anon bytes"); listBox_Logger.SelectedIndex = listBox_Logger.Items.Count - 1; }); }; // 2. Asynchronous delegate using begin/end invoke (obsolete) Robot.asyncDataProcessDelegate += ProcessDataFromBotAsync; // 3. Asynchronous delegate using TPL and await keyword Robot.asyncTplDataReceivedDelegate = async(byte[] data) => { this.Invoke((MethodInvoker) delegate { listBox_Logger_TPL.Items.Add(DateTime.Now.ToString() + " Some async/TPL bytes"); listBox_Logger_TPL.SelectedIndex = listBox_Logger_TPL.Items.Count - 1; }); return(true); }; StartParentThread(); }