在Twitch[C#]中使用IRC客户端时WPF应用程序不可用

在Twitch[C#]中使用IRC客户端时WPF应用程序不可用,c#,wpf,irc,C#,Wpf,Irc,我想用一个漂亮的用户界面来建立我自己的TwitchBot。但是我遇到了一些麻烦:我很抱歉格式化,但是它没有很好地从VisualStudio复制它,这里的格式化看起来很糟糕 public class IrcClient { private string userName; private string channel; private TcpClient tcpClient; private StreamReader inputSt

我想用一个漂亮的用户界面来建立我自己的TwitchBot。但是我遇到了一些麻烦:我很抱歉格式化,但是它没有很好地从VisualStudio复制它,这里的格式化看起来很糟糕

public class IrcClient
{
        private string userName;
        private string channel;

        private TcpClient tcpClient;
        private StreamReader inputStream;
        private StreamWriter outputStream;

        public IrcClient(string ip, int port, string userName, string password, string channel)
        {
            this.userName = userName;
            this.channel = channel;

            tcpClient = new TcpClient(ip, port);
            inputStream = new StreamReader(tcpClient.GetStream());
            outputStream = new StreamWriter(tcpClient.GetStream());

            outputStream.WriteLine($"PASS {password}");
            outputStream.WriteLine($"NICK {userName}");
            outputStream.WriteLine($"USER {userName} 8 * :{userName}");
            outputStream.WriteLine($"JOIN #{channel}");
            outputStream.Flush();
        }

        public string ReadMessage()
        {
            return inputStream.ReadLine();
        }
}

这是我在其中设置IRC客户端的类。然后我使用标准的WPF/C#内置于VisualStudio中


        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            
            System.Windows.Threading.DispatcherTimer dispatcherTimerChat = new System.Windows.Threading.DispatcherTimer();

            dispatcherTimerChat.Tick += new EventHandler(dispatcherTimerChat_tick);
            dispatcherTimerChat.Interval = new TimeSpan(500000);

            dispatcherTimerChat.Start();

            client = new IrcClient("irc.twitch.tv", 6667, "x", "x", "x");

            var pinger = new Pinger(client);
            pinger.Start();

        }

        private void dispatcherTimerChat_tick(object sender, EventArgs e)
        {
            Console.WirteLine(client.ReadMessage());
        }

加载主窗口时会调用Window_loaded。在那里,我只想接收聊天记录中的内容。但用户界面却严重滞后。当我把一些基本的东西放入XAML代码中时,比如Richttextbox,我甚至不能在那里写东西而没有一个坚实的延迟。虽然降低TimeSpan(x)会有所帮助,但在一个人口合理的频道中阅读聊天是有问题的。这里显然出了点问题,但我不知道是什么

Pinger类只是每5分钟Ping一次,所以我不会被踢出频道,而是在自己的线程上运行


这不是完整的代码,但只缺少运行WPF表单的最低要求。

多亏了我在异步编程方面的评论。这个解决方案对我有效:

我添加了一个函数

private async Task<string> getChat()
{
    string msg = await Task.Run(() => client.ReadMessage());

    return msg;
}

这对我一直都很有帮助。

欢迎来到我的某些地方,我希望。。。谢谢观看了一些教程及其现在的工作情况,再次感谢@aepot您可以用一些描述和更新的代码回答问题,并接受解决方案。在这种情况下,它可能对其他人有用。
private async void dispatcherTimerChat_tick(object sender, EventArgs e)
{
    string msg = await getChat();

    Console.WriteLine(msg);
}