Cooper Scott
Cooper Scott

Reputation: 706

Infinite For Loop Crash

Here I Have a windows app with two items:

RichTextBox chatbot

Button startButton

Okay, so when the start button is pressed some code runs and then it hits this loop:

for (buf = input.ReadLine(); ; buf = input.ReadLine())
{

    //Display received irc message
    chatbox.Text += "\n " + 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
     */
}

Boom... Program stopped responding...

When I run this code in a console program though it all works perf. Runs good, executes good, performs as expected.

What is this code apart of (Twitch Bot I am making)

Upvotes: 1

Views: 800

Answers (1)

WiXXeY
WiXXeY

Reputation: 1001

I think you have to use a BackgroundWorker to avoid the UI stucking.

public Form1()
{
    InitializeComponent();

    backgroundWorker1.DoWork += backgroundWorker1_DoWork;
    backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
}

private void button1_Click(object sender, EventArgs e)
{
    backgroundWorker1.RunWorkerAsync();
}

private void backgroundWorker1_DoWork(object sender,            System.ComponentModel.DoWorkEventArgs e)
{
     // Your infinite loop here, example is given below
     for (int i = 0; i < 100000; i++)
     {
        Console.WriteLine(i);
         Thread.Sleep(1000);
     }
}

Upvotes: 1

Related Questions