#### Archived

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

# [C#] Updating Things In Mid Loops

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

## 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 on other sites
But I don''t know how to that with C#.

Maybe someone else can help?

Cheers

My game: Swift blocks

##### Share on other sites

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

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633769
• Total Posts
3013759
×