2009-04-23 23 views
12

Según lo que he encontrado en C#, el método Control.Invoke requiere que use un delegado sin parámetros de entrada. ¿Hay alguna forma de evitar esto? Me gustaría invocar un método para actualizar la UI desde otro hilo y pasarle los parámetros de cadena.Control.Invocar con entrada Parámetros

Respuesta

22

¿Qué versión de C# estás usando? Si está usando C# 3.5, puede usar cierres para evitar pasar parámetros.

con C# 3,5
public static class ControlExtensions 
{ 
    public static TResult InvokeEx<TControl, TResult>(this TControl control, 
              Func<TControl, TResult> func) 
    where TControl : Control 
    { 
    return control.InvokeRequired 
      ? (TResult)control.Invoke(func, control) 
      : func(control); 
    } 

    public static void InvokeEx<TControl>(this TControl control, 
             Action<TControl> func) 
    where TControl : Control 
    { 
    control.InvokeEx(c => { func(c); return c; }); 
    } 

    public static void InvokeEx<TControl>(this TControl control, Action action) 
    where TControl : Control 
    { 
    control.InvokeEx(c => action()); 
    } 
} 

con seguridad invocando código ahora se convierte en trivial.

this.InvokeEx(f => f.label1.Text = "Hello World"); 
this.InvokeEx(f => this.label1.Text = GetLabelText("HELLO_WORLD", var1)); 
this.InvokeEx(() => this.label1.Text = DateTime.Now.ToString()); 

con C# 2.0 se convierte en menos trivial
public class MyForm : Form 
{ 
    private delegate void UpdateControlTextCallback(Control control, string text); 
    public void UpdateControlText(Control control, string text) 
    { 
    if (control.InvokeRequired) 
    { 
     control.Invoke(new UpdateControlTextCallback(UpdateControlText), control, text); 
    } 
    else 
    { 
     control.Text = text; 
    } 
    } 
} 

El uso sea sencillo, pero hay que definir más devoluciones de llamada para más parámetros.

this.UpdateControlText(label1, "Hello world"); 
1

creo (excelente) El enfoque de Samuel puede ser empujado aún más:

Método de extensión: Código

public static void ExecuteAsync<TControl>(this TControl control, Action action) 
where TControl : Control 
{ 
    new Thread(() => 
    { 
    control.Invoke(action); 
    }) 
    .Start(); 
} 

Forma:

private void doStuff() 
{ 
    this.ExecuteAsync(() => 
    { 
    // Do your stuff in a separate thread 
    // but having full access to local or instance variables. 

    // No (visible) threading code needs to be used here. 
    }); 
} 
6

Como Lucas dice, usa Control.I nvoke así ...

Por ejemplo, en una forma:

public delegate void DelegatePassMessages(string name, int value); 

public DelegatePassMessages passMessage; 

En el contructor:

passMessage = new DelegatePassMessages (this.MessagesIn); 

Entonces la función MessagesIn para recibir datos:

public void MessagesIn(string name, int value) 
{ 

} 

Entonces para pasar datos a su formulario:

formName.Invoke(formName.passMessage, new Object[] { param1, param2}); 
0

Encontré un método elegante para .net 2.0 con métodos anónimos envueltos en un delegado de MethodInvoker. De esa forma no es necesario definir delegados propios todo el tiempo.Ejemplo:

private void InitUI(Guid id, string typename) 
    { 
     MethodInvoker inv = delegate{tvMatrix.Nodes[0].Nodes.Add(id.ToString(), typename);}; 
     tvMatrix.Invoke(inv); 
    } 
0

Por qué no

tvMatrix.Invoke((MethodInvoker) (() => { 
    tvMatrix.Nodes[0].Nodes.Add(id.ToString(), typename); 
})); 
7

Algunos más posibilidades:

this.Invoke(new MethodInvoker(() => this.DoSomething(param1, param2))); 

o

this.Invoke(new Action(() => this.DoSomething(param1, param2))); 

o incluso

this.Invoke(new Func<YourType>(() => this.DoSomething(param1, param2))); 

donde la primera opción es la mejor, porque MethodInvoker se concibe para ese propósito y tiene un mejor rendimiento.

1

Aquí puede usar expresiones lambda con la extensión Invoke() + un parámetro de entrada.

Uso: acción (db STARS)

_ccb.GetImagerFRU_PartNbr().Invoke(new Action<STARS>(dbase => _ccb.GetImagerFRU_PartNbr().Text = dbase.PartNumber(serial) ?? String.Empty), db); 
0
private void ppTrace(string tv) 
    { 
     if (_Txb1.InvokeRequired) 
     { 
      _Txb1.Invoke((Action<string>)ppTrace, tv); 
     } 
     else 
     { 
      _Txb1.AppendText(tv + Environment.NewLine); 
     } 
    }