Hello! I have a question on threading, and though I use VB I could not find a more suitable place to post than here. My question is certainly valid for C# too. I hope it's all right.
I'm trying to start a thread using the thread pool. The method I am calling is in another object (object 2) and needs information on which external object (object 3) it should communicate with. It does this by taking a ByRef parameter when calling it. Object 3 is shared with other threads and will of course be made thread safe.
My problem is that VS2010 isn't happy with the method I'm calling, and says my method does not have a signature compatible with delegate 'Delegate Sub WaitCallback(state As Object)'. I'm guessing this is because .NET uses ByVal by default , so the delegate wishes to _create_ a state object and not merely have a reference to one. Can this little snag be overcome somehow?
In my case I can get by with coding my class a bit less flexible, directly stating which object to communicate with (object 3), and remove the ByRef from the method's declaration. However, since the class then contains no reference of the external object, the compiler starts complaining again.
How can one get around this problem?
Cheers,
-Steinar
Loading
SteinarPosted Aug 25, 2010, 12:20 PM
I left the method to be threaded with the ByRef parameter, but instead of pointing to the method directly using ThreadPool.QueueUserWorkItem, I created a new method in the calling class, a method which sole purpose is to run the ByRef method I wanted to start. This "wrapper method" is defined with a standard ByVal parameter so it can be used by the threadpool, and it can itself run the "real" method using a ByRef parameter.
I hope that was not too confusing...
Cheers again,
-Steinar