1

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)

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Cooper Scott
  • 651
  • 8
  • 18
  • 1
    The for loop needs to know under what condition to stop looping. Think about it, what would the equivalent while loop to this be? – cost Aug 12 '15 at 04:13
  • Without sharing the actual Exception and Stack Trace, it will be very difficult to pinpoint the cause of your issues. – Bishoy Aug 12 '15 at 04:15
  • 2
    Use BackgroundWorker for long running operation in background http://stackoverflow.com/questions/1216791/winform-application-ui-hangs-during-long-running-operation – WiXXeY Aug 12 '15 at 04:17
  • @WiXXeY Works perf. If you want, You can write that as a answer, get the cred – Cooper Scott Aug 12 '15 at 04:32
  • Winforms apps work differently to console apps. Winform apps are supposed to be "event driven", which means most of the time they are just spinning waiting for a message that tells them a button was clicked or whatever. If they go too long without checking for messages, Windows assumes they've frozen ("Program is not responding"). That's why you shouldn't have infinite loops in a button handler. – Blorgbeard Aug 12 '15 at 04:32

1 Answers1

1

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);
     }
}
WiXXeY
  • 981
  • 5
  • 19
  • 46
  • That's not necessarily good advice. The original code appears to set the `Text` of a control, which is something that should absolutely not be done in the `DoWork` event handler. If you do want to update the UI, make sure to call `ReportProgress` and perform the actual update in the `ProgressChanged` event handler. That way, you keep all UI access to the UI thread. – jmcilhinney Aug 12 '15 at 04:59