Trying to create a UDP Listener RRS feed

  • Question

  • I'm trying to create a UDP listener within a Windows form.  I want to listen on an address and endpoint and want to display the received text in a textbox on the form.  I'm trying to use a BackgroundWorker to allow for the listener code to keep running, while still having control in the UI thread.  I've added a BackgroundWorker to the form, so I can start and stop the listener.  I can receive the test from a sender client and display it in a MessageBox, but am having trouble updating a textbox on the form with that same text (which is what I actually want to do).  I've followed the example in help for the BackgroundWorker class, but the example displays some text in a MessageBox, but not within the form itself.  Is there a way to display the received text in a textbox on the form using a BackgroundWorkder, or should I investigate using a seperate thread for this.  Not having done any multithreaded programs, I'm a bit of a newbie in this regard.  Any help would be appreciated.  Thanks.  The code follows:


    Just so you know, where I say "ipAdd = IPAddress.Parse("my address"), that's not really what I put, I use the actuall address in my code.

    When I try to run it, and add the text to the textbox, I get the error message:

    "Error message reads: cross-thread operation not valid: Control 'monitorTextBox' accessed from a thread other than the thread it was created on."

    namespace SystemMonitor { public partial class SystemMonitorForm : Form { public class Stuff { // Passing in Text box to add text to public TextBox statusText = null; // The UdpClient to listen on public UdpClient listener = null; } private IPAddress ipAdd; private Int16 portNum; private IPEndPoint endPt; private UdpClient listener; public SystemMonitorForm() { InitializeComponent(); }

    // Cancel Button private void cancelButton_Click(object sender, EventArgs e) { SystemMonitorForm.ActiveForm.Close(); } // I want to click the start button to start the listener private void startButton_Click(object sender, EventArgs e) { //IPEndPoint senderEP = new IPEndPoint(IPAddress.Any, 0); //EndPoint tempEP = (EndPoint)senderEP; ipAdd = IPAddress.Parse("my address"); portNum = 30000; endPt = new IPEndPoint(ipAdd, portNum); listener = new UdpClient(endPt); Stuff myStuff = new Stuff(); myStuff.listener = listener; myStuff.statusText = monitorTextBox; this.backgroundWorker1.RunWorkerAsync(myStuff); } private void stopButton_Click(object sender, EventArgs e) { this.backgroundWorker1.CancelAsync(); listener.Close(); } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { // Don not access the form's BackgroundWorker reference directly. // Instead, use the reference provided by the sender parameter. BackgroundWorker bw = sender as BackgroundWorker; // Extract the arguments Stuff arg = (Stuff)e.Argument; e.Result = KeepListening(bw, ref arg); // If the operation was canceled by the user, // set the DoWorkEventArgs.Cancel porperty to true. if (bw.CancellationPending) { e.Cancel = true; } } // my current iteration passes in the listener and the textbox to append to // but that didn't work, so I commented it out private int KeepListening(BackgroundWorker bw, ref Stuff myStuff) { int result = 0; try { while (!bw.CancellationPending) { byte[] bytes = myStuff.listener.Receive(ref endPt); //myStuff.statusText.AppendText(Encoding.ASCII.GetString(bytes, 0, bytes.Length) + "\n"); MessageBox.Show(Encoding.ASCII.GetString(bytes, 0, bytes.Length) + "\n"); } } // If this doesn't work well, try catching the Exception catch (Exception ex) { MessageBox.Show("Exception caught!!!\n" + "Source : " + ex.Source + "\nMessage : " + ex.Message); } finally { listener.Close(); } return result; } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled) { MessageBox.Show("We are not longer listening!"); } else if (e.Error != null) { MessageBox.Show("An Error occured: " + e.Error.Message); } else { } } } }

    • Moved by Mike Feng Wednesday, May 15, 2013 6:51 AM
    Tuesday, May 14, 2013 10:43 PM


All replies