Change label text at runtime in wpf C#












-3















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?










share|improve this question




















  • 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
















-3















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?










share|improve this question




















  • 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














-3












-3








-3








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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








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












1 Answer
1






active

oldest

votes


















0














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);
}
});





share|improve this answer
























  • 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











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
});


}
});














draft saved

draft discarded


















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









0














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);
}
});





share|improve this answer
























  • 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
















0














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);
}
});





share|improve this answer
























  • 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














0












0








0







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);
}
});





share|improve this answer













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);
}
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 7:18









richejrichej

578215




578215













  • 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











  • 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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen