I am required to pass some sort of identifier t开发者_开发技巧o 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
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!
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.
精彩评论