Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

malpass

[C#] Updating Things In Mid Loops

This topic is 5485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

One problem i''v come across a few times now and want fixing is say you want to do some intensive coding inside for or while loops usually inside a function, ie:
public void DoSomething()
{
   while (true)
   {
      if (somethingTellsMeToBreak) break;
      else DoSomethingElse();
   }
}
 
then inside that functions while loop if at a point you want to update some text in a label or something, indicating its current progress...
public void DoSomething()
{
   while (true)
   {
      if (somethingTellsMeToBreak) break;
      else
      {
         aLabel.Text = "SomeNewText";
      }
   }
}
 
... it won''t update the text, until the function returns I presume. How can I make something update while still inside an unreturned/unfinished function.

Share this post


Link to post
Share on other sites
Advertisement
yeah, multithreading should work.

Instead of simply calling the function, try doing this:

System.Threading.Thread LongFunction = new Thread(new ThreadStart(DoSomething));
LongFunction.Start();


See if that helps...

NOTE: this assumes your function takes 0 parameters

Share this post


Link to post
Share on other sites
In this case I think you can also do an aLabel.Invalidate() or perhaps a aLabel.Refresh() after setting the text.

[edited by - MauMan on June 18, 2003 3:28:13 PM]

Share this post


Link to post
Share on other sites
You might just need to give windows some time to update the label, which can be done by calling the Sleep API function with a time of 0.

Share this post


Link to post
Share on other sites
One way to solve this is by calling Application.DoEvents():


void SomeReallyLongFunction()
{
long i;

for (i=0; i<10000000000; ++i)
{
this.label1.Text = i.ToString();
Application.DoEvents();
}
}

However, the preferred design is to do your long/intensive calculations on a worker thread. Just remember the first commandment of designing multi-threaded Windows GUI applications:

"Thou shalt not update thine UI from any thread besides the UI thread."

There is an excellent tutorial here that covers this design in C#.

[edited by - noparity on June 18, 2003 4:16:51 PM]

Share this post


Link to post
Share on other sites
I tryed the Invalidate & Refresh, but never thought to use DoEvents, that will obviously work as you use the same technique in DX. I''ll also try the multithreading too.

Tnx all

Share this post


Link to post
Share on other sites
quote:
Original post by noparity
Just remember the first commandment of designing multi-threaded Windows GUI applications:

"Thou shalt not update thine UI from any thread besides the UI thread."

But you can use Control.Invoke to marshall a call onto the right thread.



AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!