IRC Bot GUI崩溃?

时间:2014-02-06 08:22:10

标签: c# c#-3.0

好的,机器人连接得很好,但是当我创建机器人时,表单崩溃了。机器人保持连接但是!about命令也不起作用。如果我使用与控制台应用程序相同的确切代码一切正常。问题只是UI表单

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;

namespace cIRCBot
{
    public partial class bSetup : Form
    {
        public bSetup()
        {
            InitializeComponent();
        }

        private void createbotbtn_Click(object sender, EventArgs e)
        {
            string buf, nick, owner, server, chan;
            int port;
            TcpClient sock = new TcpClient();
            TextReader input;
            TextWriter output;

            //Get nick, owner, server, port, and channel from user
            nick = botnick.Text;
            owner = botname.Text;
            server = servername.Text;
            bool isNumber = int.TryParse(portnum.Text, out port);
            chan = channelname.Text;

            if (isNumber == false)
            {
                MessageBox.Show("Failed to connect. Make sure the server address and port number are correct.", "Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Connect to irc server and get input and output text streams from TcpClient.
            sock.Connect(server, port);
            if (!sock.Connected)
            {
                //Console.WriteLine("Failed to connect!");
                return;
            }
            else
            {
                this.Close();
                input = new StreamReader(sock.GetStream());
                output = new StreamWriter(sock.GetStream());

                //Starting USER and NICK login commands 
                output.Write(
                   "USER " + nick + " 0 * :" + owner + "\r\n" +
                   "NICK " + nick + "\r\n"
                );
                output.Flush();


                //Process each line received from irc server
                for (buf = input.ReadLine(); ; buf = input.ReadLine())
                {

                    //Display received irc message
                    //Console.WriteLine(buf);

                    //Send pong reply to any ping messages
                    if (buf.StartsWith("PING ")) { output.Write(buf.Replace("PING", "PONG") + "\r\n"); output.Flush(); }
                    if (buf[0] != ':') continue;

                    /* IRC commands come in one of these formats:
                     * :NICK!USER@HOST COMMAND ARGS ... :DATA\r\n
                     * :SERVER COMAND ARGS ... :DATA\r\n
                     */

                    //After server sends 001 command, we can set mode to bot and join a channel
                    if (buf.Split(' ')[1] == "001")
                    {
                        output.Write("MODE " + nick + " +B\r\n" + "JOIN " + chan + "\r\n");
                        output.Flush();
                        if (buf.Contains("!about"))
                        {

                            output.WriteLine("PRIVMSG {0} :" + "I'm a shitty little bot coded by " + botname, channelname);
                            output.Flush();
                        }
                    }
                }
            }
        }

    }
}

这是主窗口,我有另一种用于设置的窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace cIRCBot
{
    public partial class mWin : Form
    {
        public mWin()
        {
            InitializeComponent();
        }

        private void newBotToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bSetup bSetup = new bSetup();
            bSetup.Show();

        }

        private void quitToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

这是设置窗口

1 个答案:

答案 0 :(得分:0)

我怀疑造成这个问题的原因是你正在做this.Close()并且它被处理掉了。致电Close()后,您进入for()并尝试对可能处置的资源进行操作。