如何使用this.Invoke方法传递多个参数给委托

dotnet2011 2012-07-17 11:40:48
下面是MSDN中的例子:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;

public class MyFormControl : Form
{
public delegate void AddListItem(String myString);
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 myString)
{
myListBox.Items.Add(myString);
}
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();
}
}
public class MyThreadClass
{
MyFormControl myFormControl1;
public MyThreadClass(MyFormControl myForm)
{
myFormControl1 = myForm;
}
String myString;

public void Run()
{


for (int i = 1; i <= 5; i++)
{
myString = "Step number " + i.ToString() + " executed";
Thread.Sleep(400);
// Execute the specified delegate on the thread that owns
// 'myFormControl1' control's underlying window handle with
// the specified list of arguments.
myFormControl1.Invoke(myFormControl1.myDelegate,
new Object[] {myString});
}
}
}



msdn中的例子只传递了一个参数,如果需要传递两个string参数呢?比如:
public delegate void AddListItem(String myString1,String myString2);
public AddListItem myDelegate = new AddListItem(AddListItemMethod);
public void AddListItemMethod(String myStr1, String myStr2)
{
myListBox.Items.Add(myStr1+myStr2);
}


该如何用this.Invoke调用呢?试了this.Invoke(myDelegate,new object{"100","200"});不行,会出错。
...全文
1684 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
123华材 2014-09-28
  • 打赏
  • 举报
回复
就指导,我没有传参成功。。。
ppdouble 2013-11-29
  • 打赏
  • 举报
回复
String var1; Short var2; server.invoke(on, "methodinmbean", new Object[]{var1, var2}, new Object[]{String.class.getName(), Short.class.getName()}); 没试过,可以试下
ppdouble 2013-11-29
  • 打赏
  • 举报
回复
引用 4 楼 dotnet2011 的回复:
找到原因了 ,传递string 和 objcect参数都没问题,但是传递ushort就不行 有兴趣的可以试试什么原因。
http://blog.csdn.net/ppdouble/article/details/16884873 Try to this
 server.invoke(on, "methodinmbean", new Object[]{arg1, args2}, new String[]{String.class.getName(), String.class.getName()});  
dotnet2011 2012-07-17
  • 打赏
  • 举报
回复
找到原因了 ,传递string 和 objcect参数都没问题,但是传递ushort就不行 有兴趣的可以试试什么原因。
dotnet2011 2012-07-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
试过了 ,这样不行。会报错。
new object[]{"100","200"}
[/Quote]
bdmh 2012-07-17
  • 打赏
  • 举报
回复
new object[]{"100","200"}
cheng2005 2012-07-17
  • 打赏
  • 举报
回复
myFormControl1.Invoke(myFormControl1.myDelegate,
new Object[] {myString});

例子就摆在这,照着抄都能抄错,怎不知道该怎么表达。

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧