0

When I run the program I get an exception on line 54:
Cross-thread operation not valid: Control 'listBoxResult' accessed from a thread other than the thread it was created on.

Form:
------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
//-------
using System.Net;
using System.Net.Sockets;
//------
namespace ThreadDemo
{
    public partial class MainForm : Form
    {

        ChatClient cl;
        Thread updateThread;

        public MainForm()
        {
            InitializeComponent();

        }

        private void startButton_Click(object sender, EventArgs e)
        {

            cl = new ChatClient();

            // Register event handlers.
            cl.ListenModeOutput += updateTextBox;

            ThreadStart startFunc = new ThreadStart(cl.listenMode);

            updateThread = new Thread(startFunc);

            updateThread.Start();
          
        }

        private void stopButton_Click(object sender, EventArgs e)
        {
            //threadDone = true;

        }

        //target for event
        private void updateTextBox(String str)
        {

            listBoxResult.Items.Add(str);

        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }


 
    }
}

Client.cs
-----------------------------------------

//what kind of exceptions might happen

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;

namespace ThreadDemo
{
    class ChatClient
    {
        TcpClient client;
        NetworkStream stream;

        //a delegate that poins to a function that return void
        //and takes one argument
        public delegate void OutputHandler(String msg);

        //client can send this event
        public event OutputHandler ListenModeOutput;

        public ChatClient()
        {

            client = new TcpClient();

            client.Connect("localhost", 1234);

            Console.WriteLine("connected to server!");

            stream = client.GetStream();
        }


        public void listenMode()
        {
            int i = Thread.CurrentThread.ManagedThreadId;

            ListenModeOutput("listening....");
            while (true)
            {
                while (stream.DataAvailable)
                {
                    byte[] inbuffer = new byte[256];

                    int numBytesRead = stream.Read(inbuffer, 0, inbuffer.Length);

                    string inmsg = Encoding.ASCII.GetString(inbuffer, 0, numBytesRead);

                    ListenModeOutput("Server:" + inmsg);

                }
            }

        }

    }
}
2
Contributors
1
Reply
4
Views
6 Years
Discussion Span
Last Post by Momerath
1

I suspect that something in startFunc is trying to update the listbox (calling updateTextBox()). Since startFunc isn't the UI thread, it can't do this without invoking the update. Take a look at this example on MSDN.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.