Steinar

Steinar

  • NA
  • 5
  • 4.5k

ThreadPool - using a method taking a byRef parameter

Aug 25 2010 11:26 AM
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

Answers (1)