2009-04-08 21 views
11

Utilizo el siguiente código para acceder a las propiedades en mi formulario, pero hoy me gustaría escribir cosas en un ListView, que requiere más parámetros.C# Cómo invocar con más de un parámetro

public string TextValue 
    { 
     set 
     { 
      if (this.Memo.InvokeRequired) 
      { 
       this.Invoke((MethodInvoker)delegate 
       { 
        this.Memo.Text += value + "\n"; 
       }); 
      } 
      else 
      { 
       this.Memo.Text += value + "\n"; 
      } 
     } 
    } 

Cómo agregar más de un parámetro y cómo usarlos (valor, valor)?

Respuesta

27

(edición - creo que no he entendido bien la pregunta original)

Basta con que sea un método en lugar de una propiedad:

public void DoSomething(string foo, int bar) 
{ 
    if (this.InvokeRequired) { 
     this.Invoke((MethodInvoker)delegate { 
      DoSomething(foo,bar); 
     }); 
     return; 
    } 
    // do something with foo and bar 
    this.Text = foo; 
    Console.WriteLine(bar); 
} 
0

Genéricamente, Usted puede hacer que siga

  • En C# 2012/Net 4.5 Cree un proyecto de aplicación de Windows Forms llamado Lambda1
  • En el Forma M1, inserte una etiqueta llamada label1
  • Pulse F4 para abrir las propiedades Form1 (no las propiedades LABEL1)
  • Haga clic en la vista Eventos (icono con un trueno)
  • doble clic en el evento de cierre de formulario. Se creará un controlador de eventos.
  • No importa el controlador de eventos por ahora. Será reemplazado por otro más adelante;
  • Seleccione y borre todo el código en Form.cs (tecla Ctrl-A/Delete)
  • Copie y pegue el siguiente código en Form1.cs;

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Linq.Expressions; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Diagnostics; 
namespace Lambda1 
{ 
public partial class Form1 : Form 
{ 

    System.Timers.Timer t = new System.Timers.Timer(1000); 
    Int32 c = 0; 
    Int32 d = 0; 
    Func<Int32, Int32, Int32> y; 

    public Form1() 
    { 

     InitializeComponent();   
     t.Elapsed += t_Elapsed; 
     t.Enabled = true; 
    } 

    void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 
    { 
     c = (Int32)(label1.Invoke(y = (x1, x2) => 
       { label1.Text = (x1 + x2).ToString(); 
           x1++; 
           return x1; }, 
           c,d)); 
     d++; 
    } 

    private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     t.Enabled = false; 
    } 
} 

} 

Qué hacer este código es: se crea

Un temporizador. El controlador de eventos transcurrido

void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e) 

será llamado cada 1000 ms

la Label1.Text se actualizará dentro de este controlador de eventos. Sin la invocación, habrá un hilo emitida

Para actualizar el Label1.Text con un nuevo valor, se utiliza el código


c = (Int32)(label1.Invoke(y = (x1, x2) => { label1.Text = (x1 + 
x2).ToString(); x1++; return x1; }, c,d)); 

Por favor, vean que cyd están siendo pasado como argumento a x1 y x2 en la función Invocar y x1 se devuelve en la llamada Invoke.

La variable d se insertó en este código solo para mostrar cómo pasar más de una variable cuando se invoca Invoke.

Cuestiones relacionadas