views:

242

answers:

2

Hi.

I have a program in C# that connects to a server using a DuplexChannelFactory, and that server can call methods from client side by callback, the problem is that I have no idea how to access the Main Thread were the connection was created from the callback methods. Is there any way to do this, by means of passing an object, or do I have to implement an additional communication layer (BD, files, whatever)?

Hope this was not much confusing.

Thanks in advance.

A: 

I'm not sure I entirely understand your question, but I'll take a stab at it:

My understanding of the question:

You have a client and server. Your client makes a webservice call to the server, and the server can then make some sort of call back to the client in the process of calculating whatever it calculates. You have this level of infrastructure working, but you're now having difficulty communicating between the callback's state and the state of the original request.

I see a few options here, depending on the nature of the state that you're trying to share.

1) If the state that you're trying to share isn't spinning (IE, if it's known when you make the request) and isn't terribly large, I think the most sensible option would be to pass that state along with the webservice call so that the server can pass it back in to the callback.

2) If the state is spinning, you may provide access to it via some sort of icky, but threadsafe, global mechanism. Perhaps the much-maligned singleton, or something similar.

3) If the state isn't spinning, but is large, you may want to consider pre-calculating the server's request before sending the initial request.

There are a variety of techniques, of which these are only a very few, but there really isn't enough information in the question to give a good answer.

Greg D
Thanks for the reply. I really can't explain better than I did (lacking both on English and technical grammar), yet I ended up solving my problem because I have access to the CallBack object being created.Thanks again anyway.
A: 

What is your reason for wanting to access the main thread? Do you need to update a UI, and can only do that on the main thread?

In that case, look at System.Windows.Forms.Control.Invoke, which can queue a delegate to be executed on the UI thread.

John Saunders