全局字典与 GCHandle
我需要将某种标识符传递给非托管代码,然后非托管代码处理请求并在完成一些处理后回调到我的托管代码。
我想知道是否最好创建一个 GCHandle 并将其传递给非托管代码,然后在非托管代码将 GCHandle 传回后恢复该对象,或者是否最好创建一个关联的全局字典(例如整数)具有该键的对象。
感谢您的帮助!
直到
I am required to pass some sort of identifier to unmanaged code which then processes a request and calls back into my managed code once it has done some processing.
I was wondering whether it would be better to create a GCHandle and pass it to the unmanaged code to then recover the object once the unmanaged code passes the GCHandle back or whether it would be better to create a global dictionary of (say integers) which associate the object with that said key.
Thanks for the help!
Till
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我刚刚创建了一个类的一百万个实例,并通过创建随机整数键将其添加到字典中。同样,我创建了一百万个对象并为它们创建了 GCHandles。
使用 GCHandles 大约需要将对象添加到字典中的时间的 60%。
感谢您的帮助!
I have just created one million instances of a class and added it to a dictionary by creating random integer keys. Similarly, I created a million objects and created GCHandles for them.
Using GCHandles takes about 60% of the time that it takes to add the objects to the dictionary.
Thanks for the help!
如果您不必实际将托管对象传递给非托管代码,我会投票支持全局字典。使用字典的缺点可能是需要线程同步。 GCHandle 的问题是给 GC 带来额外的负担,你必须进行清理等工作。
If you don't have to actually pass the managed object to unmanaged code, I would vote for a global dictionary. Downside of using dictionary can be need for thread synchronization. Issue with GCHandle is that puts extra burden on GC and you have to do clean-up etc.