mercredi 1 avril 2015

C# update a control from an external thread rising an event

Here is the context : in Winform, I use an object from an imported library, then I launch its main method. This object rises events to give process statut while it's proceed (on which I subscribe with one of my winform method to get this statuts). The main method returns a bool, only when it has ended its process (so the events rised allow to know the evolution of the process)


Here is the problem : my method subscribed to the event is well rised, it writes into the console the statut AND it add in a listbox the statut (using BeginInvoke) The informations writen into the console are put each time the event is rised (just what I need), BUT the listbox appears blanks until the main method as returned its value, and then is finally shows its contents.


I would like the listbox (or whatever control) shows the status returned from event at the exact moment it receives it, otherwise I can't inform my user during the process.


Here is my code, could you help me ?



Local method rised by object from library :




private void ClientOnUpdate(object sender, EventParameters UpdateEventArgs)
{
//Instant write
Console.WriteLine("Update Event Parameters: {0}", UpdateEventArgs);

//Wait for the return of the Start method to show results inf lisbox
lbErrors.BeginInvoke((Action)delegate()
{
lbErrors.Items.Add(String.Format("Update Event Parameters: {0}", UpdateEventArgs));
});

}



Launching the main method of the object from the 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 get from the fired event):




  1. Start

  2. Step 1 OK

  3. Step 2 OK

  4. Step 3 OK

  5. End


Many thanks!


Aucun commentaire:

Enregistrer un commentaire