繁体   English   中英

在 C# 中调用带参数方法的最短方法

[英]Shortest way to invoke a method with parameters in C#

当我需要在指定的线程中调用一些代码时,我使用的是这样的:

Dispatcher dispatcher = Dispatcher.CurrentDispatcher;

delegate void MethodToInvokeDelegate(string foo, int bar);

void MethodToInvoke(string foo, int bar)
{
    DoSomeWork(foo);
    DoMoreWork(bar); 
}

void SomeMethod()
{
    string S = "Some text";
    int I = 1;
    dispatcher.BeginInvoke(new MethodToInvokeDelegate(MethodToInvoke), new object[] {S, I});
}

这段代码工作正常,但它很重。 我想在不声明MethodToInvokeMethodToInvokeDelegate使用匿名方法。 但是我不知道如何将参数传递给它。

我不能这样写:

dispatcher.BeginInvoke((Action)delegate() { DoSomeWork(S); DoMoreWork(I); });

我需要实际将参数传递给方法。

有没有办法把它写得简短而简单?

例子:

Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
int[] ArrayToFill = new int[3];

void SomeMethod()
{
    for (int i = 0; i < 3; i++)
        dispatcher.BeginInvoke( { ArrayToFill[i] = 10; } );
}

此代码将不起作用:将使用 i = 1、2、3 调用方法并将引发 IndexOutOfRange 异常。 i将在方法开始执行之前递增。 所以我们需要像这样重写它:

Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
int[] ArrayToFill = new int[3];

delegate void MethodToInvokeDelegate(int i);

void MethodToInvoke(int i)
{
    ArrayToFill[i] = 10; 
}

void SomeMethod()
{
    for (int i = 0; i < 3; i++)
         dispatcher.BeginInvoke(new MethodToInvokeDelegate(MethodToInvoke), new object[] {i});
}

如果您希望避免为每个调用创建委托类型,请使用Action<>Action<,>

Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
string S = "Some text";
int I = 1;
dispatcher.BeginInvoke(
                        (Action<string, int>)((foo, bar) =>
                        {
                            MessageBox.Show(bar.ToString(), foo);
                            //DoSomeWork(foo);
                            //DoMoreWork(bar); 
                        }), 
                        new object[] { S, I }
                      );

ArrayToFill[j] = 10;的示例ArrayToFill[j] = 10; 动作可以很简单地修复:

for (int i = 0; i < 3; i++)
{
    int j = i;
    // lambda captures int variable
    // use a new variable j for each lambda to avoid exceptions
    dispatcher.BeginInvoke(new Action(() =>
    {
        ArrayToFill[j] = 10;
    }));
}

尝试这个:

string S = "Some text";
int I = 1;

dispatcher.BeginInvoke(new Action(() =>
{
    DoSomeWork(S);
    DoMoreWork(I);
}));

[编辑]

针对您修改后的问题:

您看到的问题是已修改的关闭问题

要修复它,您只需要在调用方法之前复制参数:

Dispatcher dispatcher = Dispatcher.CurrentDispatcher;

int[] ArrayToFill = new int[3];

for (int i = 0; i < 3; i++)
{
    int index = i;
    dispatcher.BeginInvoke(new Action(() => { ArrayToFill[index] = 10; } ));
}
yourUIcontrol.BeginInvoke(new MethodInvoker(delegate {
    //your code goes here
}));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM