I'm using a SDK for a usb camera. The SDK specifies that for each frame grabbed, a callback function will be called. The callback function is defined inside the SDK, it gets a data pointer to the image and a structure used to interpret the data.
我正在使用SDK用于USB摄像头。 SDK指定对于每个抓取的帧,将调用回调函数。回调函数在SDK中定义,它获取指向图像的数据指针和用于解释数据的结构。
All of that works correctly.
所有这一切都正常。
To make a useful application out of that, I need to access a few variables from my application. Now because the delegate function is static, I can only access static members. I thought of making a singleton out of them because its gonna be static, but is there any "conventionnal way" of accessing other data inside a delegate function?
为了创建一个有用的应用程序,我需要从我的应用程序中访问一些变量。现在因为委托函数是静态的,我只能访问静态成员。我想过用它们制作一个单例因为它是静态的,但是在委托函数中是否有任何“常规方式”访问其他数据?
3 个解决方案
#1
Why not just use a non-static delegate? You'd have access to the class instance members in that case. Is there something forcing you into the static delegate instead of a delegate on an instance of one of the class where your data and logic resides?
为什么不使用非静态委托?在这种情况下,您可以访问类实例成员。有没有什么东西迫使你进入静态委托而不是你的数据和逻辑所在的类之一的实例上的委托?
If so, then is there a way to pass data to the callback? If you can, you could pass a reference to a class, and use that in your delegate to get your application data.
如果是这样,那么有没有办法将数据传递给回调?如果可以,您可以传递对类的引用,并在您的委托中使用它来获取您的应用程序数据。
If not, then you may be forced to have some static data, or a static reference to a class holding your data. A singleton or similar construct may be the best option in this case...
如果没有,那么您可能*拥有一些静态数据,或者对包含数据的类的静态引用。在这种情况下,单身或类似构造可能是最好的选择......
#2
Why did you make the delegate a static? If it hurts when you do it, stop doing it. :)
你为什么让委托成为静态的?如果你这样做会疼,那就停止这样做吧。 :)
#3
The delegate can be static (though why it would need to be, I'm not clear). The function does not need to be.
委托可以是静态的(虽然它为什么需要,我不清楚)。该功能不需要。
Dereferencing a delegate into a method call just doesn't care whether the function called is an instance method or a static method.
将委托解除引用到方法调用中并不关心所调用的函数是实例方法还是静态方法。
#1
Why not just use a non-static delegate? You'd have access to the class instance members in that case. Is there something forcing you into the static delegate instead of a delegate on an instance of one of the class where your data and logic resides?
为什么不使用非静态委托?在这种情况下,您可以访问类实例成员。有没有什么东西迫使你进入静态委托而不是你的数据和逻辑所在的类之一的实例上的委托?
If so, then is there a way to pass data to the callback? If you can, you could pass a reference to a class, and use that in your delegate to get your application data.
如果是这样,那么有没有办法将数据传递给回调?如果可以,您可以传递对类的引用,并在您的委托中使用它来获取您的应用程序数据。
If not, then you may be forced to have some static data, or a static reference to a class holding your data. A singleton or similar construct may be the best option in this case...
如果没有,那么您可能*拥有一些静态数据,或者对包含数据的类的静态引用。在这种情况下,单身或类似构造可能是最好的选择......
#2
Why did you make the delegate a static? If it hurts when you do it, stop doing it. :)
你为什么让委托成为静态的?如果你这样做会疼,那就停止这样做吧。 :)
#3
The delegate can be static (though why it would need to be, I'm not clear). The function does not need to be.
委托可以是静态的(虽然它为什么需要,我不清楚)。该功能不需要。
Dereferencing a delegate into a method call just doesn't care whether the function called is an instance method or a static method.
将委托解除引用到方法调用中并不关心所调用的函数是实例方法还是静态方法。