How to change TextBlock.Text every loop?
up vote
3
down vote
favorite
This is UWP C# application. I want to display the i variable.
private void btn2_Click(object sender, RoutedEventArgs e)
{
for(int i = 0; i<=10000000; i++)
{
textBlock2.Text = i.ToString();
}
}
But after I clicked the button, the text only change one time after the loop is finished.
How can see changing text in every loop?
c# uwp
add a comment |
up vote
3
down vote
favorite
This is UWP C# application. I want to display the i variable.
private void btn2_Click(object sender, RoutedEventArgs e)
{
for(int i = 0; i<=10000000; i++)
{
textBlock2.Text = i.ToString();
}
}
But after I clicked the button, the text only change one time after the loop is finished.
How can see changing text in every loop?
c# uwp
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
This is UWP C# application. I want to display the i variable.
private void btn2_Click(object sender, RoutedEventArgs e)
{
for(int i = 0; i<=10000000; i++)
{
textBlock2.Text = i.ToString();
}
}
But after I clicked the button, the text only change one time after the loop is finished.
How can see changing text in every loop?
c# uwp
This is UWP C# application. I want to display the i variable.
private void btn2_Click(object sender, RoutedEventArgs e)
{
for(int i = 0; i<=10000000; i++)
{
textBlock2.Text = i.ToString();
}
}
But after I clicked the button, the text only change one time after the loop is finished.
How can see changing text in every loop?
c# uwp
c# uwp
edited Nov 8 at 5:17
John
10.5k31734
10.5k31734
asked Nov 8 at 5:15
peachy__kat
265
265
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Actually, your UI is frozen while the long loop is executing, so nothing can be updated on the screen. Try this
private async void btn2_Click(object sender, RoutedEventArgs e)
{
for(int i = 0; i<=10000000; i++)
{
textBlock2.Text = i.ToString();
await Task.Delay(200); //stay 200 ms before showing next number so human eyes can see it.
}
}
When it executes to this line await Task.Delay(200);
, the loop is temporarily suspended - for 200 milliseconds, and the UI thread now has a chance to process the drawing tasks pending in the queue, which includes redrawing the textblock.
1
Better to useBackgroundWorker
here, let the UI thread do its work and background thread do its work. Mixing between logic is not a good practice.
– Barr J
Nov 8 at 5:23
2
BackgroundWorker was an invention in WinForms era...I think we don't use it anymore.
– kennyzx
Nov 8 at 5:25
10000000 * 200 ms ~ 23 days. This will be a long for-loop:)
– vasily.sib
Nov 8 at 6:05
@kennyzx: BackgroundWorker is in System.ComponentModel and resides in System.dll. Can be used basically anywhere, regardless of actual technology (and supports reporting progress). But since async-await exists its relevance declined greatly.
– taffer
Nov 8 at 6:36
I have just learned that it is still in .NET Core and .NET standard, pretty surprised. It was “invented” - as I called - to hide multi-threading from developers, just put you code inside the DoWork method like Btn_Click, and maybe it is kept to make the life easier for those who need to maintain old codebase.
– kennyzx
Nov 8 at 6:43
|
show 3 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Actually, your UI is frozen while the long loop is executing, so nothing can be updated on the screen. Try this
private async void btn2_Click(object sender, RoutedEventArgs e)
{
for(int i = 0; i<=10000000; i++)
{
textBlock2.Text = i.ToString();
await Task.Delay(200); //stay 200 ms before showing next number so human eyes can see it.
}
}
When it executes to this line await Task.Delay(200);
, the loop is temporarily suspended - for 200 milliseconds, and the UI thread now has a chance to process the drawing tasks pending in the queue, which includes redrawing the textblock.
1
Better to useBackgroundWorker
here, let the UI thread do its work and background thread do its work. Mixing between logic is not a good practice.
– Barr J
Nov 8 at 5:23
2
BackgroundWorker was an invention in WinForms era...I think we don't use it anymore.
– kennyzx
Nov 8 at 5:25
10000000 * 200 ms ~ 23 days. This will be a long for-loop:)
– vasily.sib
Nov 8 at 6:05
@kennyzx: BackgroundWorker is in System.ComponentModel and resides in System.dll. Can be used basically anywhere, regardless of actual technology (and supports reporting progress). But since async-await exists its relevance declined greatly.
– taffer
Nov 8 at 6:36
I have just learned that it is still in .NET Core and .NET standard, pretty surprised. It was “invented” - as I called - to hide multi-threading from developers, just put you code inside the DoWork method like Btn_Click, and maybe it is kept to make the life easier for those who need to maintain old codebase.
– kennyzx
Nov 8 at 6:43
|
show 3 more comments
up vote
3
down vote
accepted
Actually, your UI is frozen while the long loop is executing, so nothing can be updated on the screen. Try this
private async void btn2_Click(object sender, RoutedEventArgs e)
{
for(int i = 0; i<=10000000; i++)
{
textBlock2.Text = i.ToString();
await Task.Delay(200); //stay 200 ms before showing next number so human eyes can see it.
}
}
When it executes to this line await Task.Delay(200);
, the loop is temporarily suspended - for 200 milliseconds, and the UI thread now has a chance to process the drawing tasks pending in the queue, which includes redrawing the textblock.
1
Better to useBackgroundWorker
here, let the UI thread do its work and background thread do its work. Mixing between logic is not a good practice.
– Barr J
Nov 8 at 5:23
2
BackgroundWorker was an invention in WinForms era...I think we don't use it anymore.
– kennyzx
Nov 8 at 5:25
10000000 * 200 ms ~ 23 days. This will be a long for-loop:)
– vasily.sib
Nov 8 at 6:05
@kennyzx: BackgroundWorker is in System.ComponentModel and resides in System.dll. Can be used basically anywhere, regardless of actual technology (and supports reporting progress). But since async-await exists its relevance declined greatly.
– taffer
Nov 8 at 6:36
I have just learned that it is still in .NET Core and .NET standard, pretty surprised. It was “invented” - as I called - to hide multi-threading from developers, just put you code inside the DoWork method like Btn_Click, and maybe it is kept to make the life easier for those who need to maintain old codebase.
– kennyzx
Nov 8 at 6:43
|
show 3 more comments
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Actually, your UI is frozen while the long loop is executing, so nothing can be updated on the screen. Try this
private async void btn2_Click(object sender, RoutedEventArgs e)
{
for(int i = 0; i<=10000000; i++)
{
textBlock2.Text = i.ToString();
await Task.Delay(200); //stay 200 ms before showing next number so human eyes can see it.
}
}
When it executes to this line await Task.Delay(200);
, the loop is temporarily suspended - for 200 milliseconds, and the UI thread now has a chance to process the drawing tasks pending in the queue, which includes redrawing the textblock.
Actually, your UI is frozen while the long loop is executing, so nothing can be updated on the screen. Try this
private async void btn2_Click(object sender, RoutedEventArgs e)
{
for(int i = 0; i<=10000000; i++)
{
textBlock2.Text = i.ToString();
await Task.Delay(200); //stay 200 ms before showing next number so human eyes can see it.
}
}
When it executes to this line await Task.Delay(200);
, the loop is temporarily suspended - for 200 milliseconds, and the UI thread now has a chance to process the drawing tasks pending in the queue, which includes redrawing the textblock.
edited Nov 8 at 5:36
answered Nov 8 at 5:20
kennyzx
9,66542263
9,66542263
1
Better to useBackgroundWorker
here, let the UI thread do its work and background thread do its work. Mixing between logic is not a good practice.
– Barr J
Nov 8 at 5:23
2
BackgroundWorker was an invention in WinForms era...I think we don't use it anymore.
– kennyzx
Nov 8 at 5:25
10000000 * 200 ms ~ 23 days. This will be a long for-loop:)
– vasily.sib
Nov 8 at 6:05
@kennyzx: BackgroundWorker is in System.ComponentModel and resides in System.dll. Can be used basically anywhere, regardless of actual technology (and supports reporting progress). But since async-await exists its relevance declined greatly.
– taffer
Nov 8 at 6:36
I have just learned that it is still in .NET Core and .NET standard, pretty surprised. It was “invented” - as I called - to hide multi-threading from developers, just put you code inside the DoWork method like Btn_Click, and maybe it is kept to make the life easier for those who need to maintain old codebase.
– kennyzx
Nov 8 at 6:43
|
show 3 more comments
1
Better to useBackgroundWorker
here, let the UI thread do its work and background thread do its work. Mixing between logic is not a good practice.
– Barr J
Nov 8 at 5:23
2
BackgroundWorker was an invention in WinForms era...I think we don't use it anymore.
– kennyzx
Nov 8 at 5:25
10000000 * 200 ms ~ 23 days. This will be a long for-loop:)
– vasily.sib
Nov 8 at 6:05
@kennyzx: BackgroundWorker is in System.ComponentModel and resides in System.dll. Can be used basically anywhere, regardless of actual technology (and supports reporting progress). But since async-await exists its relevance declined greatly.
– taffer
Nov 8 at 6:36
I have just learned that it is still in .NET Core and .NET standard, pretty surprised. It was “invented” - as I called - to hide multi-threading from developers, just put you code inside the DoWork method like Btn_Click, and maybe it is kept to make the life easier for those who need to maintain old codebase.
– kennyzx
Nov 8 at 6:43
1
1
Better to use
BackgroundWorker
here, let the UI thread do its work and background thread do its work. Mixing between logic is not a good practice.– Barr J
Nov 8 at 5:23
Better to use
BackgroundWorker
here, let the UI thread do its work and background thread do its work. Mixing between logic is not a good practice.– Barr J
Nov 8 at 5:23
2
2
BackgroundWorker was an invention in WinForms era...I think we don't use it anymore.
– kennyzx
Nov 8 at 5:25
BackgroundWorker was an invention in WinForms era...I think we don't use it anymore.
– kennyzx
Nov 8 at 5:25
10000000 * 200 ms ~ 23 days. This will be a long for-loop:)
– vasily.sib
Nov 8 at 6:05
10000000 * 200 ms ~ 23 days. This will be a long for-loop:)
– vasily.sib
Nov 8 at 6:05
@kennyzx: BackgroundWorker is in System.ComponentModel and resides in System.dll. Can be used basically anywhere, regardless of actual technology (and supports reporting progress). But since async-await exists its relevance declined greatly.
– taffer
Nov 8 at 6:36
@kennyzx: BackgroundWorker is in System.ComponentModel and resides in System.dll. Can be used basically anywhere, regardless of actual technology (and supports reporting progress). But since async-await exists its relevance declined greatly.
– taffer
Nov 8 at 6:36
I have just learned that it is still in .NET Core and .NET standard, pretty surprised. It was “invented” - as I called - to hide multi-threading from developers, just put you code inside the DoWork method like Btn_Click, and maybe it is kept to make the life easier for those who need to maintain old codebase.
– kennyzx
Nov 8 at 6:43
I have just learned that it is still in .NET Core and .NET standard, pretty surprised. It was “invented” - as I called - to hide multi-threading from developers, just put you code inside the DoWork method like Btn_Click, and maybe it is kept to make the life easier for those who need to maintain old codebase.
– kennyzx
Nov 8 at 6:43
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53201919%2fhow-to-change-textblock-text-every-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown