|
-
September 23rd, 2008, 03:50 PM
#1
(Part 2) Retriving data from a irc server
Hello.
Well since my last topic (http://www.codeguru.com/forum/showth...94#post1763094) I have read quite a bit about threads, and I feel like I know it know, but I still have one question regarding data from a irc server.
I dont feel like running a while in my thread since it doesnt seem to be very stable, what other methods is recommended or is threadding the only way?
Here is what I got so far:
Code:
#region Variables
Thread addItemThread = null;
delegate void addItemDelegate();
TcpClient irc;
NetworkStream stream;
StreamReader reader;
StreamWriter writer;
#endregion
// Opdater list boks via invoke og en delegate
public void updateListBox()
{
while (reader.ReadLine() != null)
{
Invoke(new addItemDelegate(addItemToListBox));
}
}
// Sæt reader.Readline i listbox
public void addItemToListBox()
{
listBoxConsole.Items.Add(reader.ReadLine());
}
public void connect(string server, int port)
{
irc = new TcpClient(server, port);
stream = irc.GetStream();
reader = new StreamReader(stream);
writer = new StreamWriter(stream);
writer.WriteLine("USER CSharpBot 8 * :I'm a C# irc bot");
writer.Flush();
writer.WriteLine("NICK csharp12");
writer.Flush();
writer.WriteLine("JOIN #testchannel");
writer.Flush();
}
private void btnConnect_Click(object sender, System.EventArgs e)
{
connect("irc.chatcafe.net", 6667);
addItemThread = new Thread(new ThreadStart(updateListBox));
addItemThread.Start();
}
The most important lines are at:
Code:
while (reader.ReadLine() != null)
{
Invoke(new addItemDelegate(addItemToListBox));
}
Any help appreciated
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
|