public void LimelightUpdate(object sender, DoWorkEventArgs e)
 {
     while (true)
     {
         LimelightWidget.CheckStream();
         Thread.Sleep(2000);
     }
 }
        public void Update(object sender, DoWorkEventArgs e)
        {
            // Receive loop from Sharp OSC.
            while (true)
            {
                try
                {
                    // If the receiver exists, then receive the packet. Skip if it's null.
                    if (Receiver == null)
                    {
                        Thread.Sleep(1);
                        continue;
                    }

                    OscPacket Packet = Receiver.Receive();

                    if (Packet == null)
                    {
                        Thread.Sleep(1);
                        continue;
                    }

                    if (Packet.GetType().Name == "OscMessage")
                    {
                        // Get the next message. This receive will not block.
                        OscMessage ReceivedMessage = (OscMessage)Packet;

                        // Show any received Limelight values.
                        if (ReceivedMessage.Address.Equals("/Robot/Limelight/X"))
                        {
                            Application.Current.Dispatcher.InvokeAsync(new Action(() => LimelightWidget.UpdateX((double)ReceivedMessage.Arguments[0])));
                        }
                        if (ReceivedMessage.Address.Equals("/Robot/Limelight/Y"))
                        {
                            Application.Current.Dispatcher.InvokeAsync(new Action(() => LimelightWidget.UpdateY((double)ReceivedMessage.Arguments[0])));
                        }
                        if (ReceivedMessage.Address.Equals("/Robot/Limelight/A"))
                        {
                            Application.Current.Dispatcher.InvokeAsync(new Action(() => LimelightWidget.UpdateA((double)ReceivedMessage.Arguments[0])));
                        }

                        // Show any received Console values.
                        if (ReceivedMessage.Address.Equals("/Robot/Console/Text"))
                        {
                            Application.Current.Dispatcher.InvokeAsync(new Action(() => ConsoleBox.PrintLine((string)ReceivedMessage.Arguments[0])));
                        }
                    }
                    else
                    {
                        // The packet is actually a bundle, and should be logged.
                        OscBundle Bundle = (OscBundle)Packet;

                        // Log the bundle data if the bundle ID contains the word log.
                        if (((string)Bundle.Messages[0].Arguments[0]).Contains("Log"))
                        {
                            // Create the relevant data for the CSV file.
                            string BundleIdentifier = "";
                            string HeaderLine       = "";
                            string DataLine         = "";

                            // Iterate through all the messages and generate the header and data rows.
                            foreach (OscMessage Message in Bundle.Messages)
                            {
                                if (Message.Address.Equals("/BundleIdentifier"))
                                {
                                    BundleIdentifier = (string)Message.Arguments[0];
                                    continue;
                                }

                                HeaderLine += Message.Address + ",";
                                DataLine   += ((double)Message.Arguments[0]).ToString() + ",";
                            }

                            // Call the log data function.
                            Application.Current.Dispatcher.InvokeAsync(new Action(() => LoggerWidget.LogData(BundleIdentifier, HeaderLine, DataLine)));
                        }

                        // If the bundle ID is "CurrentBundle" then send the data to the current widget.
                        if (((string)Bundle.Messages[0].Arguments[0]).Equals("CurrentBundle"))
                        {
                            // Iterate through all the messages and generate the header and data rows.
                            foreach (OscMessage Message in Bundle.Messages)
                            {
                                if (!Message.Address.Equals("/BundleIdentifier"))
                                {
                                    Application.Current.Dispatcher.InvokeAsync(new Action(() => CurrentWidget.SetCurrentMeter((double)Message.Arguments[0], Message.Address)));
                                }
                            }
                        }

                        // If the bundle ID is "SensorInputBundle" then send the data to the current widget.
                        if (((string)Bundle.Messages[0].Arguments[0]).Equals("SensorInputBundle"))
                        {
                            // Iterate through all the messages and generate the header and data rows.
                            foreach (OscMessage Message in Bundle.Messages)
                            {
                                if (!Message.Address.Equals("/BundleIdentifier"))
                                {
                                    Application.Current.Dispatcher.InvokeAsync(new Action(() => SensorInputWidget.SetSensorValue((double)Message.Arguments[0], Message.Address)));
                                }
                            }
                        }

                        // If the bundle ID is "ErrorBundle" then send the data to the error reporter.
                        if (((string)Bundle.Messages[0].Arguments[0]).Equals("ErrorBundle"))
                        {
                            List <string> Errors = new List <string>();

                            // Iterate through all the messages and generate the header and data rows.
                            foreach (OscMessage Message in Bundle.Messages)
                            {
                                if (!Message.Address.Equals("/BundleIdentifier"))
                                {
                                    foreach (int Argument in Message.Arguments)
                                    {
                                        if (Argument != -1)
                                        {
                                            Errors.Add(ConvertErrorAddress(Message.Address) + ConvertFault(Argument));
                                        }
                                    }
                                }
                            }

                            Application.Current.Dispatcher.InvokeAsync(new Action(() => ErrorWidget.SetErrors(Errors)));
                        }

                        if (((string)Bundle.Messages[0].Arguments[0]).Equals("ControllerDataBundle"))
                        {
                            // Iterate through all the messages and save controller data.
                            foreach (OscMessage Message in Bundle.Messages)
                            {
                                switch (Message.Address)
                                {
                                case "/BundleIdentifier":
                                    break;

                                case "/DriverController/Buttons":

                                    foreach (int ButtonState in Message.Arguments)
                                    {
                                    }
                                    break;

                                case "/DriverController/Axis":
                                    foreach (double AxisValue in Message.Arguments)
                                    {
                                    }
                                    break;

                                case "/OperatorController/Buttons":
                                    foreach (int ButtonState in Message.Arguments)
                                    {
                                    }
                                    break;

                                default:
                                    break;
                                }
                            }
                        }
                    }
                }
                catch (Exception Ex)
                {
                    // Catch any exceptions.
                    MessageBox.Show(Ex.Message + Ex.StackTrace, "OSC Receive Exception", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
                }
            }
        }