I want to create an IRC connection but I was having trouble because the connection was not using ConnectAsync
. It is now using ConnectAsync
but the UI is still blocked.
I would appreciate it if someone can help me with working example code for a non-blocking IRC connection that I can read/write from/to as I am struggling to understand asynchronous code.
async Task Connect()
{
using (var client = new TcpClient())
{
await client.ConnectAsync("127.0.0.1", 1234);
using (var stream = client.GetStream())
using (var writer = new StreamWriter(stream))
using (var reader = new StreamReader(stream))
{
writer.AutoFlush = true;
writer.WriteLine("USER Test 0 * :Test");
writer.WriteLine("NICK Test");
while (client.Connected)
{
var data = reader.ReadLine();
if (data != null)
{
var d = data.Split(' ');
textBox1.AppendText($"Data: {data}" + Environment.NewLine);
if (d[0] == "PING")
{
writer.WriteLine("PONG");
}
}
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
Connect();
}