Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am looking for a way to free up the thread which updates the form while doing a back ground task. I started back with VB6 and used a method called DoEvents. Like most VB6 things was this a hack or a proper method. I have encountered a similar situtatin to the VB6 one in C# and am pondering the use of it in the form Application.DoEvents as below:

C#
for(int a = 0 ; a<=100; a++)
         {
             textBox1.Text = a.ToString();
             Application.DoEvents();
 //some heavy time consuming task such as reading data from a serial port
             Thread.Sleep(250);
         }


Is this bad, is there a better way to free up the form?

Glenn
Posted

1 solution

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:
C#
    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)
            {
            //Do your long running task
            work.ReportProgress(50);
            }
        }
    }
 
Share this answer
 
Comments
glennPattonWork3 2-Apr-13 6:55am    
Ummm, OK looking into that, why is DoEvents evil? like I said I came across it in VB6 solved a problem forgot about it....
OriginalGriff 2-Apr-13 7:15am    
IN VB6 (And most pre-.NET languages) DOEvents was the only easy way to get the message loop emptied (and the UI responsive) while a long operation was going on - threading was difficult to implement and generally a PITA. So calling DoEvents let the message loop run and do all the pending repaints before coming back.

But...it isn't a good solution:
"DoEvents messes up the normal flow of your application. If I recall correctly, DoEvents is asynchronous which means it terminates before the application has actually processed any outstanding events, so if you're using it in a procedure with many sequential statements, calling DoEvents causes a huge disturbance whenever it's called. Basically, if you find yourself needing to call DoEvents anywhere, think about starting another thread instead, or using asynchronous delegates.

Imagine this if you will: You have a button on your form that, when clicked, does some complex processing. During the complex processing it also intermittently calls DoEvents to keep the application's user interface "responsive" -- not the best method, I would have used async delegates, but we're talking about a mediocre programmer here. Anyhow, the user sees the application still responding and no indication that there's some processing going on. So the user clicks that button again WHILE the processing is going on! The button responds to the event and starts another processing thread but it isn't actually a thread here, I hope you get what I'm saying. So, like I said earlier, DoEvents screws up the flow of the application too easily." Dan Tohatan (but the original linked document is dead but it's still a well phrased explanation)

Now, we have BackgroundWorker which is simple to use, and other pretty easy methods, so there isn't a good argument for using DoEvents. And in the world of multiple core CPUs, starting a new thread means there is a good chance of things going faster because the OS can move the execution to a "spare" core and it can happen truly in parallel!
glennPattonWork3 2-Apr-13 7:23am    
So,DoEvents bad, threading good.
I can handle that.
OriginalGriff 2-Apr-13 8:26am    
Short and to the point! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900