I'm new to using delegates and invoke methods. Can someone please quickly identify what I'm missing here. I know this code is no where near completed right now, but as is, it's working for what I'm trying to test. Basically I'm creating a simple client application that is receiving a server message. Connections are fine, and the thread is receiving the server message. I just need to know how to properly marshal the message to my Form class control from my Client class.
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.
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);
//calc.Added += new CalculatorFunctions.AnswerFunc(mathLibrary_Added);
}
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
}