|
-
Dec 6th, 2007, 09:02 AM
#1
Thread Starter
Member
Getting data from Thread/BackgroundWorker
Hi. I'm building a P2Pish chat client for a class project, but I'm having a little trouble with the threading. The chat program basically consists of a server and client. The server doesn't actually handle the conversations, the clients do it themselves. The server is only used to register users as being online when they log on and stores connection information for that user. When one client wants to talk to someone on its friends list, it gets the connection information from the server for that person and proceeds to make a direct connection to that client.
This isn't completely relevant to the question, its just a bit of background information so you can understand what I'm trying to do.
That part of it works fine. Here's the problem: I have a chat window that I bring up to talk to a friend. I type text in it, that text is sent down the stream via TcpClient and read by the other client (looping through a thread). The problem I'm having is that once the receiver gets the message from the stream he needs to append that message to the textBox in his own chat window (which pops up on an incoming connection).
Since the reading of messages for this conversation (handled within the ChatSession class, created for each conversation), is in a thread I can't simply do something like this:
Code:
StreamReader sr = new StreamReader(_endToEndConnection.GetStream());
while (true)
{
Thread.Sleep(10);
if ((line = sr.ReadLine()) != "")
{
txtChat.AppendText(line);
}
}
because I do not have access to the txtChat control within the thread.
I've been trying to user BackgroundWorker, having it execute the above code and fire the RunWorkerCompleted event. Unfortunately, that only executes when the DoWork function actually exits, and since it's looping repeatedly, waiting for new incoming messages, it never really quits.
Any suggestions on how I can update that txtChat control from a thread?
-
Dec 6th, 2007, 05:27 PM
#2
Fanatic Member
Re: Getting data from Thread/BackgroundWorker
You should be able to use the backgroundWorker.ProgressChanged event to pass information to the text box.
Setup the backgroundWorker to listen to the ProgressChanged event and also set it it report progress.
Instead of appending the line to text box pass it to the backgroundWorker.ReportProgress method which will fire the ProgressChanged event an allow you to update controls.
Code:
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
StreamReader sr = new StreamReader(_endToEndConnection.GetStream());
while (true && !backgroundWorker.CancellationPending)
{
int lineCount = 0;
Thread.Sleep(10);
if ((line = sr.ReadLine()) != "")
{
lineCount++;
backgroundWorker.ReportProgress(lineCount, line);
}
}
}
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
txtChat.AppendText (e.UserState.ToString());
}
There are also several backgroundWorker examples floating around on the forums.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|