|
Cet article a fait l'objet d'une traduction automatique. Déplacez votre pointeur sur les phrases de l'article pour voir la version originale de ce texte. Informations supplémentaires.
|
Traduction
Source
|
Control.Invoke, méthode (Delegate)
Espace de noms : System.Windows.Forms
Assembly : System.Windows.Forms (dans System.Windows.Forms.dll)
Paramètres
- method
- Type : System.Delegate
Délégué qui contient une méthode à appeler dans le contexte du thread du contrôle.
Valeur de retour
Type : System.Object Remarque |
|---|
Remarque |
|---|
/* The following example demonstrates the 'Invoke(Delegate)' method of 'Control class. A 'ListBox' and a 'Button' control are added to a form, containing a delegate which encapsulates a method that adds items to the listbox.This function is executed on the thread that owns the underlying handle of the form. When user clicks on button the above delegate is executed using 'Invoke' method. */ using System; using System.Drawing; using System.Windows.Forms; using System.Threading; public class MyFormControl : Form { public delegate void AddListItem(); public AddListItem myDelegate; private Button myButton; private Thread myThread; private ListBox myListBox; public MyFormControl() { myButton = new Button(); myListBox = new ListBox(); myButton.Location = new Point(72, 160); myButton.Size = new Size(152, 32); myButton.TabIndex = 1; myButton.Text = "Add items in list box"; myButton.Click += new EventHandler(Button_Click); myListBox.Location = new Point(48, 32); myListBox.Name = "myListBox"; myListBox.Size = new Size(200, 95); myListBox.TabIndex = 2; ClientSize = new Size(292, 273); Controls.AddRange(new Control[] {myListBox,myButton}); Text = " 'Control_Invoke' example"; myDelegate = new AddListItem(AddListItemMethod); } static void Main() { MyFormControl myForm = new MyFormControl(); myForm.ShowDialog(); } public void AddListItemMethod() { String myItem; for(int i=1;i<6;i++) { myItem = "MyListItem" + i.ToString(); myListBox.Items.Add(myItem); myListBox.Update(); Thread.Sleep(300); } } private void Button_Click(object sender, EventArgs e) { myThread = new Thread(new ThreadStart(ThreadFunction)); myThread.Start(); } private void ThreadFunction() { MyThreadClass myThreadClassObject = new MyThreadClass(this); myThreadClassObject.Run(); } } // The following code assumes a 'ListBox' and a 'Button' control are added to a form, // containing a delegate which encapsulates a method that adds items to the listbox. public class MyThreadClass { MyFormControl myFormControl1; public MyThreadClass(MyFormControl myForm) { myFormControl1 = myForm; } public void Run() { // Execute the specified delegate on the thread that owns // 'myFormControl1' control's underlying window handle. myFormControl1.Invoke(myFormControl1.myDelegate); } }
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (rôle principal du serveur non pris en charge), Windows Server 2008 R2 (rôle principal du serveur pris en charge avec SP1 ou version ultérieure ; Itanium non pris en charge)
Le .NET Framework ne prend pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.
Remarque