C#如何调用多个参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/729430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# How to invoke with more than one parameter
提问by Ivan Prodanov
I use the code below to access the properties on my form,but today I'd like to write stuff to a ListView,which requires more parameters.
我使用下面的代码来访问表单上的属性,但今天我想将内容写入 ListView,这需要更多参数。
public string TextValue
{
set
{
if (this.Memo.InvokeRequired)
{
this.Invoke((MethodInvoker)delegate
{
this.Memo.Text += value + "\n";
});
}
else
{
this.Memo.Text += value + "\n";
}
}
}
How to add more than one parameter and how to use them(value,value)?
如何添加多个参数以及如何使用它们(值,值)?
采纳答案by Marc Gravell
(edit- I think I misunderstood the original question)
(编辑- 我想我误解了原来的问题)
Simply make it a method instead of a property:
简单地使它成为一个方法而不是一个属性:
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);
}
回答by Antonio Leite
Generically, You can do as follow
通常,您可以执行以下操作
- In C# 2012/Net 4.5 Create a Windows Forms Application project called Lambda1
- In the Form1 Form, insert a Label called label1
- Press F4 to open the Form1 properties (not the label1 properties)
- Click on the Events view (Icon with a thunder)
- Double click on the Form Closing event. An event handler will be created.
- Don't mind about the event handler for now. It will be replaced by another one later;
- Select and erase all the code in Form.cs (Ctrl-A/Delete key)
- Copy and paste the following code to Form1.cs;
- 在 C# 2012/Net 4.5 中创建一个名为 Lambda1 的 Windows 窗体应用程序项目
- 在 Form1 表单中,插入一个名为 label1 的 Label
- 按 F4 打开 Form1 属性(不是 label1 属性)
- 单击事件视图(带有雷声的图标)
- 双击 Form Closing 事件。将创建一个事件处理程序。
- 现在不要介意事件处理程序。稍后会被另一个替换;
- 选择并擦除 Form.cs 中的所有代码(Ctrl-A/Delete 键)
- 将以下代码复制并粘贴到 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;
}
}
}
What this code do is:
这段代码的作用是:
A timer is created. The Elapsed Event Handler
创建了一个计时器。Elapsed 事件处理程序
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
will be called every 1000ms
将每 1000 毫秒调用一次
The label1.Text will be updated inside this event handler. Without the Invoke, there will be a thread issued
label1.Text 将在此事件处理程序中更新。没有Invoke,就会有线程发出
To update the label1.Text with a new value, the code was used
要使用新值更新 label1.Text,使用了代码
c = (Int32)(label1.Invoke(y = (x1, x2) => { label1.Text = (x1 +
x2).ToString(); x1++; return x1; }, c,d));
Please see that c and d are being passed as argument to x1 and x2 in the the Invoke function and x1 is returned in the Invoke call.
请注意 c 和 d 作为参数传递给 Invoke 函数中的 x1 和 x2,而 x1 在 Invoke 调用中返回。
The variable d was inserted in this code just to show how to pass more than one variable when Invoke is called.
在这段代码中插入变量 d 只是为了展示在调用 Invoke 时如何传递多个变量。