Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Process messages in long loops

Status
Not open for further replies.

majkinetor

Programmer
Feb 21, 2006
88
RS
How can I process messages in long loops, like this fictional example:
Code:
private void OnButtonClick(...)
{
   int i = 0
   while (1)
   {
      //do some calc here
      
      listBox1.Add( "OPeration " i++.ToString() )
      Applicatin.ProcessMessages();
   }
}

Application.ProcessMessages() is used in Delphi for same purpose. What should I use in c#

Thx.
 
The problem is that GUI will not be updated while in the loop as it prevents message handling loop.
 
I think you would be better off investigating a threading solution, but the equivalent to Delphi's ProcessMessages is .DoEvents.


Hope this helps.

[vampire][bat]
 
That is exactly what I need.

Thx for your suggestions but it would be overkill in my solution to even think about multithreading. I just need to update some stats :)


Thx earthandfire.
 
The threading solution sounds right to me.

Everyone gets scared of threads but they are really quite simple and they make things a lot easier. It's not overkill - this isn't c++ :)
 
C# 2005 even has the background worker thread object -- makes it much easier to use.

Only thing to remember is not to try & update the UI from the worker thread -- use a delegate to return completion status.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hm...

You may be right but I still think it is an overkill.

If I have some lenty operation I prefer to update myGui only with single call to Application.DoEvents(). Can't be simpler than that.

OFC you are right if some performance critical peace of code is in question, other thread (and delegate) can be used but in this case I have simple window with button witch when pressed do some kind of non-OO algorythm.

I see your approach as typical example of trying to use particular programming paradigm at any cost. Maybe I am wrong, I am open to any constructive criticism...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top