multithreading - How to make labels change in a C#.NET GUI-project -


i'm making application friend's birthday, window changing compliments supposed pop up. window freezes, however, , labels don't change. googled it, , read using backgroundworker in order seperate gui-thread changing process. still doesn't work.

this code:

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.threading.tasks; using system.windows.forms; using system.threading;  namespace projectl {     public partial class form1 : form     {         private messagehandler thehandler = new messagehandler();         private backgroundworker thebackgroundworker = new backgroundworker();         public form1()         {             initializecomponent();         }          private void startbutton_click(object sender, eventargs e)         {             startbutton.visible = false;             thebackgroundworker.dowork += new doworkeventhandler(thebackgroundworker_doyourwork);             //thebackgroundworker.runworkercompleted += new runworkercompletedeventhandler(thebackgroundworker_doyourwork);             thebackgroundworker.runworkerasync();                      thehandler.runmessage(hbdlabel, youarelabel, mainlabel, this);         }          void thebackgroundworker_doyourwork(object sender, doworkeventargs e)         {             thehandler.runmessage(hbdlabel, youarelabel, mainlabel, this);         }     } } 

this what's supposed happen background, using class i've named messagehandler:

class messagehandler {     public list<string> generatecomplimenttexts()     {         list<string> stringlist = new list<string>();         //adding bunch of compliments list<string>          return stringlist;     }      public void runmessage(label hbdlabel, label youarelabel, label mainlabel, form1 form)     {         list<string> stringlist = generatecomplimenttexts();         thread.sleep(2000);         form.text = "happy birthday goose!!!";         hbdlabel.text = "happy birthday goose!";         thread.sleep(3000);         youarelabel.text = "you are...";         thread.sleep(2000);         foreach (string e in stringlist)         {             mainlabel.text = e;             //form.test = e              thread.sleep(1000);         }         thread.sleep(3000);         mainlabel.text = "";         youarelabel.text = finalmessage;     }      private string _finalmessage = "final message";     public string finalmessage {get {return _finalmessage;}} } 

still, nothing changes on window. pretty frozen, except text in top-bar of form itself, if choose uncomment form.text = e;

any advice?

you achieve using timers instead of backgroundworker.

it :

mainform :

public partial class form1 : form {     private timer timer;     private messagehandler thehandler = new messagehandler();      public form1()     {         initializecomponent();     }      private void form1_load(object sender, eventargs e)     {         timer = new timer();         timer.interval = 1000;         timer.tick += timerontick;         // initialize other labels static text here     }      private void timerontick(object sender, eventargs eventargs)     {         thehandler.shownext(label1);     }      private void startbutton_click(object sender, eventargs e)     {         timer.start();     } } 

and messagehandler class :

public class messagehandler {     private list<string> compliments = new list<string>();     private int index = 0;      public messagehandler()     {         generatecomplimenttexts();     }      private void generatecomplimenttexts()     {         list<string> stringlist = new list<string>();         //adding bunch of compliments list<string>         compliments = stringlist;     }      public void shownext(label label)     {         label.text = compliments.elementat(index);         index = (index >= compliments.count - 1) ? 0 : index + 1;     } } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -