Change label text at runtime in wpf C#
I have one label named as lblMsg
.
I want to change it's content runtime dynamically.
Below is the example of my code not the actual code. My for loop contains so many code and it takes approx 8 to 9 seconds on each loop. So it should be visible on UI.
For ex.
for(int i=0;i<=length;i++)
{
lblMsg.Content="Test"+i;
}
But it is not working. Can any one help me with it?
c# wpf label wpf-controls
|
show 2 more comments
I have one label named as lblMsg
.
I want to change it's content runtime dynamically.
Below is the example of my code not the actual code. My for loop contains so many code and it takes approx 8 to 9 seconds on each loop. So it should be visible on UI.
For ex.
for(int i=0;i<=length;i++)
{
lblMsg.Content="Test"+i;
}
But it is not working. Can any one help me with it?
c# wpf label wpf-controls
1
For loop works instantly. if you want to see it updating by the time start with adding a timer and put i++ on the timer tick
– Halil İbrahim
Nov 23 '18 at 6:50
It probably does update, but it goes way too fast for you to notice (probably less then one milisecond).
– Freek W.
Nov 23 '18 at 6:51
Instantly? One millisecond? For all we knowlength = int.MaxValue
. UIs are going to hate that and if Telerik is involved well...better get out War and Peace
– MickyD
Nov 23 '18 at 6:53
@MickyD Like I said twice, probably. Not definitely.
– Freek W.
Nov 23 '18 at 7:03
1
The above code is just an exmaple. My actual code takes time to execute .. approax 8-9 secinds on each. And my label should change accordingly like. Task 1 completed on first loop, task 2 completed on second loop etc.
– Ripal
Nov 23 '18 at 9:13
|
show 2 more comments
I have one label named as lblMsg
.
I want to change it's content runtime dynamically.
Below is the example of my code not the actual code. My for loop contains so many code and it takes approx 8 to 9 seconds on each loop. So it should be visible on UI.
For ex.
for(int i=0;i<=length;i++)
{
lblMsg.Content="Test"+i;
}
But it is not working. Can any one help me with it?
c# wpf label wpf-controls
I have one label named as lblMsg
.
I want to change it's content runtime dynamically.
Below is the example of my code not the actual code. My for loop contains so many code and it takes approx 8 to 9 seconds on each loop. So it should be visible on UI.
For ex.
for(int i=0;i<=length;i++)
{
lblMsg.Content="Test"+i;
}
But it is not working. Can any one help me with it?
c# wpf label wpf-controls
c# wpf label wpf-controls
edited Nov 23 '18 at 9:11
Ripal
asked Nov 23 '18 at 6:47
RipalRipal
94
94
1
For loop works instantly. if you want to see it updating by the time start with adding a timer and put i++ on the timer tick
– Halil İbrahim
Nov 23 '18 at 6:50
It probably does update, but it goes way too fast for you to notice (probably less then one milisecond).
– Freek W.
Nov 23 '18 at 6:51
Instantly? One millisecond? For all we knowlength = int.MaxValue
. UIs are going to hate that and if Telerik is involved well...better get out War and Peace
– MickyD
Nov 23 '18 at 6:53
@MickyD Like I said twice, probably. Not definitely.
– Freek W.
Nov 23 '18 at 7:03
1
The above code is just an exmaple. My actual code takes time to execute .. approax 8-9 secinds on each. And my label should change accordingly like. Task 1 completed on first loop, task 2 completed on second loop etc.
– Ripal
Nov 23 '18 at 9:13
|
show 2 more comments
1
For loop works instantly. if you want to see it updating by the time start with adding a timer and put i++ on the timer tick
– Halil İbrahim
Nov 23 '18 at 6:50
It probably does update, but it goes way too fast for you to notice (probably less then one milisecond).
– Freek W.
Nov 23 '18 at 6:51
Instantly? One millisecond? For all we knowlength = int.MaxValue
. UIs are going to hate that and if Telerik is involved well...better get out War and Peace
– MickyD
Nov 23 '18 at 6:53
@MickyD Like I said twice, probably. Not definitely.
– Freek W.
Nov 23 '18 at 7:03
1
The above code is just an exmaple. My actual code takes time to execute .. approax 8-9 secinds on each. And my label should change accordingly like. Task 1 completed on first loop, task 2 completed on second loop etc.
– Ripal
Nov 23 '18 at 9:13
1
1
For loop works instantly. if you want to see it updating by the time start with adding a timer and put i++ on the timer tick
– Halil İbrahim
Nov 23 '18 at 6:50
For loop works instantly. if you want to see it updating by the time start with adding a timer and put i++ on the timer tick
– Halil İbrahim
Nov 23 '18 at 6:50
It probably does update, but it goes way too fast for you to notice (probably less then one milisecond).
– Freek W.
Nov 23 '18 at 6:51
It probably does update, but it goes way too fast for you to notice (probably less then one milisecond).
– Freek W.
Nov 23 '18 at 6:51
Instantly? One millisecond? For all we know
length = int.MaxValue
. UIs are going to hate that and if Telerik is involved well...better get out War and Peace– MickyD
Nov 23 '18 at 6:53
Instantly? One millisecond? For all we know
length = int.MaxValue
. UIs are going to hate that and if Telerik is involved well...better get out War and Peace– MickyD
Nov 23 '18 at 6:53
@MickyD Like I said twice, probably. Not definitely.
– Freek W.
Nov 23 '18 at 7:03
@MickyD Like I said twice, probably. Not definitely.
– Freek W.
Nov 23 '18 at 7:03
1
1
The above code is just an exmaple. My actual code takes time to execute .. approax 8-9 secinds on each. And my label should change accordingly like. Task 1 completed on first loop, task 2 completed on second loop etc.
– Ripal
Nov 23 '18 at 9:13
The above code is just an exmaple. My actual code takes time to execute .. approax 8-9 secinds on each. And my label should change accordingly like. Task 1 completed on first loop, task 2 completed on second loop etc.
– Ripal
Nov 23 '18 at 9:13
|
show 2 more comments
1 Answer
1
active
oldest
votes
If you just want to display a label with a incrementing number you can create a Task and then use a delay (Thread.Sleep()) to give the UI time to refresh the label.
Because you cannot change UI Elements within a separate Thread, you have to update the UI with the UI Dispatcher.
Sample Code
var length = 1000;
Task.Run(() =>
{
for (int i = 0; i <= length; i++)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() => {
lblMsg.Content = "Test" + i;
}), DispatcherPriority.Render);
Thread.Sleep(100);
}
});
Inter-thread marshalling over the message pump is costly. Why not just use a timer?
– MickyD
Nov 23 '18 at 8:11
So without dispatcher I can't update the label on runtime?
– Ripal
Nov 23 '18 at 9:14
@Ripal you can, but you have to give the UI some time to refresh. As MickyD mentioned you can do this with a timer.
– richej
Nov 23 '18 at 11:00
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53441840%2fchange-label-text-at-runtime-in-wpf-c-sharp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you just want to display a label with a incrementing number you can create a Task and then use a delay (Thread.Sleep()) to give the UI time to refresh the label.
Because you cannot change UI Elements within a separate Thread, you have to update the UI with the UI Dispatcher.
Sample Code
var length = 1000;
Task.Run(() =>
{
for (int i = 0; i <= length; i++)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() => {
lblMsg.Content = "Test" + i;
}), DispatcherPriority.Render);
Thread.Sleep(100);
}
});
Inter-thread marshalling over the message pump is costly. Why not just use a timer?
– MickyD
Nov 23 '18 at 8:11
So without dispatcher I can't update the label on runtime?
– Ripal
Nov 23 '18 at 9:14
@Ripal you can, but you have to give the UI some time to refresh. As MickyD mentioned you can do this with a timer.
– richej
Nov 23 '18 at 11:00
add a comment |
If you just want to display a label with a incrementing number you can create a Task and then use a delay (Thread.Sleep()) to give the UI time to refresh the label.
Because you cannot change UI Elements within a separate Thread, you have to update the UI with the UI Dispatcher.
Sample Code
var length = 1000;
Task.Run(() =>
{
for (int i = 0; i <= length; i++)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() => {
lblMsg.Content = "Test" + i;
}), DispatcherPriority.Render);
Thread.Sleep(100);
}
});
Inter-thread marshalling over the message pump is costly. Why not just use a timer?
– MickyD
Nov 23 '18 at 8:11
So without dispatcher I can't update the label on runtime?
– Ripal
Nov 23 '18 at 9:14
@Ripal you can, but you have to give the UI some time to refresh. As MickyD mentioned you can do this with a timer.
– richej
Nov 23 '18 at 11:00
add a comment |
If you just want to display a label with a incrementing number you can create a Task and then use a delay (Thread.Sleep()) to give the UI time to refresh the label.
Because you cannot change UI Elements within a separate Thread, you have to update the UI with the UI Dispatcher.
Sample Code
var length = 1000;
Task.Run(() =>
{
for (int i = 0; i <= length; i++)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() => {
lblMsg.Content = "Test" + i;
}), DispatcherPriority.Render);
Thread.Sleep(100);
}
});
If you just want to display a label with a incrementing number you can create a Task and then use a delay (Thread.Sleep()) to give the UI time to refresh the label.
Because you cannot change UI Elements within a separate Thread, you have to update the UI with the UI Dispatcher.
Sample Code
var length = 1000;
Task.Run(() =>
{
for (int i = 0; i <= length; i++)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() => {
lblMsg.Content = "Test" + i;
}), DispatcherPriority.Render);
Thread.Sleep(100);
}
});
answered Nov 23 '18 at 7:18
richejrichej
578216
578216
Inter-thread marshalling over the message pump is costly. Why not just use a timer?
– MickyD
Nov 23 '18 at 8:11
So without dispatcher I can't update the label on runtime?
– Ripal
Nov 23 '18 at 9:14
@Ripal you can, but you have to give the UI some time to refresh. As MickyD mentioned you can do this with a timer.
– richej
Nov 23 '18 at 11:00
add a comment |
Inter-thread marshalling over the message pump is costly. Why not just use a timer?
– MickyD
Nov 23 '18 at 8:11
So without dispatcher I can't update the label on runtime?
– Ripal
Nov 23 '18 at 9:14
@Ripal you can, but you have to give the UI some time to refresh. As MickyD mentioned you can do this with a timer.
– richej
Nov 23 '18 at 11:00
Inter-thread marshalling over the message pump is costly. Why not just use a timer?
– MickyD
Nov 23 '18 at 8:11
Inter-thread marshalling over the message pump is costly. Why not just use a timer?
– MickyD
Nov 23 '18 at 8:11
So without dispatcher I can't update the label on runtime?
– Ripal
Nov 23 '18 at 9:14
So without dispatcher I can't update the label on runtime?
– Ripal
Nov 23 '18 at 9:14
@Ripal you can, but you have to give the UI some time to refresh. As MickyD mentioned you can do this with a timer.
– richej
Nov 23 '18 at 11:00
@Ripal you can, but you have to give the UI some time to refresh. As MickyD mentioned you can do this with a timer.
– richej
Nov 23 '18 at 11:00
add a comment |
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.
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%2f53441840%2fchange-label-text-at-runtime-in-wpf-c-sharp%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
1
For loop works instantly. if you want to see it updating by the time start with adding a timer and put i++ on the timer tick
– Halil İbrahim
Nov 23 '18 at 6:50
It probably does update, but it goes way too fast for you to notice (probably less then one milisecond).
– Freek W.
Nov 23 '18 at 6:51
Instantly? One millisecond? For all we know
length = int.MaxValue
. UIs are going to hate that and if Telerik is involved well...better get out War and Peace– MickyD
Nov 23 '18 at 6:53
@MickyD Like I said twice, probably. Not definitely.
– Freek W.
Nov 23 '18 at 7:03
1
The above code is just an exmaple. My actual code takes time to execute .. approax 8-9 secinds on each. And my label should change accordingly like. Task 1 completed on first loop, task 2 completed on second loop etc.
– Ripal
Nov 23 '18 at 9:13