Is there a ProcessMessages in .net?

  • Thread starter Thread starter Rob Pollard
  • Start date Start date
R

Rob Pollard

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'
 
| 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.
 
Willy said:
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.

I can see the value in using DoEvents in simple cases that don't
warrant the complexity of threading. It works fine as long as you
disable your UI buttons to prevent reentrancy into your OnClick event
handlers.

Sometimes the best and most elegant solutions don't make sense in a
simple and small app. I do this kind of thing in small utility programs
that are mostly single-purpose in nature.

And this is an attractive option if you're porting code from Delphi and
you don't want to re-invent the wheel. But this may also be a good time
to revisit the program's architecture, especially if this is an
important program that is a key part of a production system.

That's just my opinion, of course.

Eric
 
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
 
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.



| 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
|
| > | > | 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.
| >
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top