简体   繁体   English

在C#或VB.NET中的std :: bind等价物

[英]std::bind equivalent in C# or VB.NET

I am in the process of refactoring "synchronous" code (ie uses Windows events to wait until some other thread finished doing something) to "asynchronous" code (using delegates to implement a callback mechanism). 我正在重构“同步”代码(即使用Windows事件等待其他线程完成某些操作)到“异步”代码(使用委托来实现回调机制)。

In the sync code, I sometimes have local variables that I need to use after the wait is over. 在同步代码中,我有时会在等待结束后使用局部变量。 When code like that goes async, those local variables are lost (the callback handler can't access them). 当像这样的代码异步时,那些局部变量就会丢失(回调处理程序无法访问它们)。 I can store them as class attributes, but it feels wasteful. 我可以将它们存储为类属性,但感觉很浪费。

In C++, I use std::bind to work around this. 在C ++中,我使用std::bind来解决这个问题。 I just add as many parameters as local variables needed to the callback handler, and bind them when I call the async method. 我只需要添加与回调处理程序所需的局部变量一样多的参数,并在调用异步方法时绑定它们。 For example, let's say that the async method callback receives an object of type CallbackParam and the caller uses two local variables of type LocalA and LocalB . 例如,假设异步方法回调接收CallbackParam类型的对象,并且调用者使用LocalALocalB类型的两个局部变量。

void AsyncClass::MethodWhichCallsAsyncMethod(){
    LocalA localVarA;
    LocalB localVarB;
    // OnAsyncMethodDone will need localVarA and localVarB, so we bind them
    AsyncMethod( std::bind( &AsyncClass::OnAsyncMethodDone, this, std::placeholders::_1, localVarA, localVarB ) );
}

void AsynClass::AsyncMethod( std::function<void(CallbackParam)> callback ){
    CallbackParam result;
    //Compute result...
    if( callback )
        callback( result );
}

void AsyncClass::OnAsyncMethodDone( CallbackParam p, LocalA a, LocalB b ){
   //Do whatever needs to be done
}

Is there some sort of equivalent to this in C# and VB.NET? 在C#和VB.NET中是否存在某种等价物? Using delegates or something else? 使用代表或其他什么?

UPDATE : For completeness' sake, here is the C# equivalent of my example based on @lasseespeholt's answer: 更新 :为了完整性,这里是基于@ lasseespeholt答案的我的例子的C#等价物:

using System;

public class AsyncClass {

        public void MethodWhichCallsAsyncMethod() {
            var a = new LocalA();
            var b = new LocalB();
            //Anonymous callback handler (equivalent to AsyncClass::OnAsyncMethodDone)
            Action<CallbackParam> callback = result => {
                //Do what needs to be done; result, a and b can be accessed
            };
            AsyncMethod( callback );
        }

        private void AsyncMethod( Action<CallbackParam> callback ) {
            var result = new CallbackParam();
            //Compute result...
            if( callback != null )
                callback( result );
        }
}

UPDATE: This should almost certainly not be used. 更新:这几乎肯定不会被使用。 Use the async/await keywords in C# 在C#中使用async / await关键字

You can exploit closures like the following: 您可以利用以下关闭:

void MethodWhichCallsAsyncMethod()
{
    int foo = 1;

    AsyncCallback callback = result =>
    {
        Console.WriteLine(foo); // Access to foo
    };

    AsyncMethod(callback);
}

void AsyncMethod(AsyncCallback callback)
{
    IAsyncResult result = null; // Compute result
    callback(result);
}

The compiler generates a class which contains "foo" so you don't save anything with this approach, but it's clean. 编译器生成一个包含“foo”的类,所以你不用这种方法保存任何东西,但它很干净。

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

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