15 mars 2007
4
15
/03
/mars
/2007
18:37
+
1 – Delegate – récupérer un résultat
a-
// 1 public delegate int ActionHandler(); // 2 public ActionHandler oActionHandler; |
b-
IAsyncResult oIAsyncResult; // 3 on definie la méthode appelée par le delegué oActionHandler = new ActionHandler(DoWork); // 4 on lance oIAsyncResult = oActionHandler.BeginInvoke(PostResult, null); |
a- Il faut obligatoirement passé en paramètre IAsyncResult
public void PostResult(IAsyncResult oIAsyncResult) { if (oIAsyncResult.IsCompleted == true) { int nResult = oActionHandler.EndInvoke(oIAsyncResult); MessageBox.Show(nResult.ToString()); } } |
- Exemple complet
public partial class FreturnDelegate : Form { public FreturnDelegate() { InitializeComponent(); } // 1 public delegate int ActionHandler(); // 2 public ActionHandler oActionHandler; // EventHandler de la progression public delegate void PostDataEventHandler(int i); private void button1_Click(object sender, EventArgs e) { IAsyncResult oIAsyncResult; // 3 on definie la méthode appelée par le delegué oActionHandler = new ActionHandler(DoWork); // 4 on lance oIAsyncResult = oActionHandler.BeginInvoke(PostResult, null); } ///<summary> /// Cette méthode n'est appelée qu'une seule fois , à la fin du traitement du delegue ///</summary> ///<param name="oIAsyncResult">passage de argument IAsyncResult oIAsyncResult obligatoire</param> public void PostResult(IAsyncResult oIAsyncResult) { if (oIAsyncResult.IsCompleted == true) { int nResult = oActionHandler.EndInvoke(oIAsyncResult); MessageBox.Show(nResult.ToString()); } } ///<summary> /// méthode qui simule un traitement long ///</summary> public int DoWork() { int nDuration = 0; for (int i = 0; i <= 5; i++) { System.Threading.Thread.Sleep(500); nDuration += 500; // 4 methode qui permettra d'atteindre les controls de la form sans avoir de probleme d'acces inter-thread this.Invoke(new PostDataEventHandler(PostData), i); } return nDuration; } public void PostData(int i) { this.progressBar1.PerformStep(); } } |
2 - S’abonner aux événements d’une classe
a-La classe
public class ClassWithEvents { // 1 delegate public delegate void StartActionEventHandler(string sMessage); // 2 eventNameEventHandler public StartActionEventHandler oStartActionEventHandler; public delegate void StopActionEventHandler(); public StopActionEventHandler oStopActionEventHandler; public ClassWithEvents() { } // // 1 Method qui fait action - operation // public void DoAction() { //debut if(oStartActionEventHandler!=null) oStartActionEventHandler.BeginInvoke("Start",null, null); // traitement // fin if(oStopActionEventHandler!=null) oStopActionEventHandler.BeginInvoke(null, null); } // 2 Methodes de l'event public void StartAction(string sMessage) { if (oStartActionEventHandler != null) { // ici on appelle la method de la meme classe // mais on pourrait tres bien propager l'event pour une classe abonnée oStartActionEventHandler(sMessage); } } public void StopAction() { } } |
b-La classe abonnée
public partial class FAbonneClassEvents : Form { public FAbonneClassEvents() { InitializeComponent(); } public delegate void PostMessage(string sMessage); private void button1_Click(object sender, EventArgs e) { ClassWithEvents oClassWithEvents; oClassWithEvents = new ClassWithEvents(); // abonnnement à l’evenement StartActionEvenetHandler oClassWithEvents.oStartActionEventHandler += new ClassWithEvents.StartActionEventHandler(PostEvent); // Opération oClassWithEvents.DoAction(); } public void PostEvent(string sMessage) { // } } |