It's the 'received.Invoke(response);' that isn't right somehow.
The form control is attempting to receive the message but it can't, because the marshalling isn't working. ('Cross-thread operation not valid:' error)
Maybe I need a second delegate for something??
HELP!!!
I've only added the code directly dealing with the thread.
Form code snippets:
public partial class frmChat : Form
{
// create new client object
private Client client = new Client();
public frmChat()
{
InitializeComponent();
client.received += new Client.receiveFunc(chatClientLibrary_Receieved);
}
private void menuItemConnect_Click(object sender, EventArgs e)
{
client.execute();
}
// method for outuptting message to textbox
private void chatClientLibrary_Receieved(string msg)
{
txtBoxConversation.Text = msg;
}
}
Client Class Code snippets:
public delegate void receiveFunc(string msg);
public event receiveFunc received;
Thread receiveThread = new Thread(new ThreadStart(threadListen));
receiveThread.Start();
private void threadListen()
{
NetworkStream stream = this.client.GetStream(); // object to receive series of bytes
byte[] inbuffer = new byte[256];
bool done = false;
//while (stream.DataAvailable)
while (!done)
{
while (stream.DataAvailable)
{
int numRead = stream.Read(inbuffer, 0, inbuffer.Length);
string response = Encoding.ASCII.GetString(inbuffer, 0, numRead);
if (response == "Quit")
{
done = true;
}
else
{
received.Invoke(response);
}
}
} // end while instream.DataAvailable
}
This post has been edited by nsjoe: 24 January 2010 - 07:33 PM

New Topic/Question
Reply




MultiQuote



|