Well it looks like we will have to agree to disagree, I keep calling it a
hack, I never used it in production code.
You simply have to ask yourself why you ever would call DoEvents, given you
know the dangers of DoEvents, because there are almost always better
alternatives.
For tivial calculations you can use anonymous delegates like this:
private void someButton_Click(object sender, EventArgs e)
{
new Thread(delegate()
{
Thread.CurrentThread.IsBackground = true;
// long running calculation here
// update UI when needed, but remember you need to delegate the
call to the UI thread!
}).Start();
}
Nothing complicated, and I don't see it as overkill, it nicely separates the
UI thread tasks from your other application tasks.
The only time I would ever call DoEvents is when I have COM STA objects
running in a thread of a non windows application (remember STA's need to
pump the queue), but even then, there are better alternatives when running
managed code.
Willy.
"Cyril Gupta" <cy***@nospam.comwrote in message
news:a9*************************@msnews.microsoft. com...
| Hello Willy Denoyette [MVP],
|
| I disagree with Application.Doevents being a hack. Creating a separate
thread
| for a trivial calculation seems like overkill to me. Of course now in .Net
| spawning a separate thread is easier, but I still think
Application.Doevent
| can help you in a lot of situation, and you shouldn't be stopped from
using
| it if you observe that the effect works for you.
|
| Regards
| Cyril Gupta
|
| You can do anything with a little bit of 'magination.
| I have been programming so long, that my brain is now soft-ware.
|
http://www.cyrilgupta.com/blog
|
| "Rob Pollard" <ro************@hotmail.comwrote in message
| news:JX*********************@fe3.news.blueyonder.c o.uk...
| | Hi All,
| | Part of my new app does some quite intensive calculations. In
| Delphi (my
| | previous language) one could call Application.ProcessMessages; in
| the
| | calculation loop to make sure that the system was being given enough
| time
| to
| | process it's own messages (makes the app and desktop much more
| responsive).
| | Is there a similiar trick available to c# .net developers?
| |
| | Thanks
| | RobP
| | 'There are only 10 types of people in this world - Those that
| understand
| | binary and those that don't'
| |
| |
| Don't rely on tricks, use a separate thread to execute long running
| tasks. If you do prefer tricks, take a look at Application.DoEvents,
| but beware it's a hack invented to help single threaded applications
| to keep the UI responsive, stay away from it if you can.
| >
| Willy.
| >
|
|