c# - Update a control from an external thread rising an event -


here context : in winform, use object imported library, launch main method. object rises events give process statut while it's proceed (on subscribe 1 of winform method statuts). main method returns bool, when has ended process (so events rised allow know evolution of process)

here problem : method subscribed event rised, writes console statut , add in listbox statut (using begininvoke) informations writen console put each time event rised (just need), but listbox appears blanks until main method returned value, , shows contents.

i listbox (or whatever control) shows status returned event @ exact moment receives it, otherwise can't inform user during process.

here code, me ?

local method rised object library :

        private void clientonupdate(object sender, eventparameters updateeventargs)     {         //instant write         console.writeline("update event parameters: {0}", updateeventargs);          //wait return of start method show results inf lisbox         lberrors.begininvoke((action)delegate()         {             lberrors.items.add(string.format("update event parameters: {0}", updateeventargs));         });      } 

launching main method of object library:

        private void launch()     {         lberrors.items.clear();         client cl1 = new client();         bool res = cl1.start();          if (res == true)         {             //do stuff ...         }         else         {             //do stuff ...         }     } 

the results (status fired event):

  1. start
  2. step 1 ok
  3. step 2 ok
  4. step 3 ok
  5. end

the reason see list items appear after executing launch private void launch() runs synchronous , blocks ui thread. listbox updated when ui thread ready executing method.

can use backgroundworker or task run launch method in. free ui thread. if possible, can make launch method async too.

for example using task:

task.run(() => launch()); 

note calling ui inside task needs invoke or begininvoke too.


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -