Click to See Complete Forum and Search --> : long loop problem


billconan
June 15th, 2009, 11:50 PM
hello guys,

i'm writing a windows application. once the user click on a button, i will run a loop. the loop will take a long time to finish, and during the execution the program looks like dead. i want my application can still response to user input, what do i do?

thanks.

plus, i saw a warning every time i run the program saying that the program doesn't respond. everything is actually ok, i just click continue, everything will be fine. but the warning is still annoying. how should i deal with that.

the warning shows up when i debug the code. i don't know if it will still show up if i compile it as a release mode.

MNovy
June 16th, 2009, 12:23 AM
This is quite simple,
just use the BackgroundWorker Thread, or the "usual" threads as follows:


using System;
using System.Text;
using System.Windows.Forms;

using System.Threading;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private void MyThread()
{
// do something loop-ish here
}

private void button1_Click(object sender, EventArgs e)
{
Thread thread = new Thread(new ThreadStart(MyThread));
thread.Start();
}

}
}



The thread runs independently and does not freeze your GUI.

BigEd781
June 16th, 2009, 02:06 AM
Understand the OS (http://www.winprog.org/tutorial/message_loop.html)

memeloo
June 20th, 2009, 02:34 PM
this tutorial is great, now I finally understand how the messages work, thx BigEd781