This is bad. You should (almost) never have to call DoEvents!
Instead, look at moving the long running task into a different thread so it doesn't affect the UI. The BackgroundWorker class is a good first choice:
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.RunWorkerAsync();
Console.ReadLine();
}
static void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
static void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
}
static void worker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker work = sender as BackgroundWorker;
if (work != null)
{
while (true)
{
work.ReportProgress(50);
}
}
}