Geeks With Blogs

News

Series

Add to Technorati Favorites


An Archived Managed World This blog has moved to http://www.managed-world.com/blog

For any of you becoming interested in Indigo development, you would do yourself some good by becoming familiar with the UIContext object. Why? Well, that will take many more words the first sentence.

For anyone familiar with multi-threaded programming, you are probably already familiar with the concept of Contexts. For those not familiar with multi-threaded programming, let me explain. In the world of Indigo, you might have to deal with Contexts in many places. One is if you derive your own MessageHandler from the SyncMessageHandler. Another might be if you create your own client “callback” interface (maybe by deriving an interface from IDialogPortTypeChannel and then providing any ServiceMessages to communicate with the client).

SyncMessageHandler

You can develop a synchronous message handler in Indigo by deriving your own class from SyncMessageHandler and overriding the ProcessMessage method. If you are wanting to communicate with an Avalon UI though, you need to be aware that when the ProcessMessage is called, the call will come in on a different thread than the UI. To mitigate this issue, your MessageHandler must know the UIContext that the UI is running on. To get the current UIContext (if your MessageHandler “knows about” the Avalon Window), you could always just call the Context property on any of the controls in the window. However, this is not very scalable. The better way, in my opinion, is to pass the UIContext into the MessageHandler's constructor or through a property on the MessageHandler class.

Once you have the the current UIContext, you need to call the BeginInvoke method to invoke a method in your class *in* the context of the UI. This allows you to update and change properties on the UI, and have the changes reflected in your running program. If you don't do this, it will appear that your UI is never being updated when in fact, it *is* being updated, just in a different UIContext. To see how to use the BeginInvoke method, please refer to the Longhorn SDK. *Note: to see it in person, there is an example of how to use it in VB on the MSDN-TV episode, “Lap Around Longhorn” with Don Box and Chris Anderson.

IDialogPortTypeChannel

This one can be a little more simple to solve. One thing that I love about the Dialog capabilities of Indigo, is that if you derive your client interface from IDialogPortTypeChannel, you can have the ServiceMessages show up as events in the class that is produced for that service by wsdlgen. For instance, if I have the following interface:

public interface IClient: IDialogPortTypeChannel
{
    [ServiceMessage]
    void MessageReceived(string from, string message);
}

and I pass that interface into all my ServiceMessages on my service, then wsdlgen will generate MessageReceived as an event that my client side code subscribes to. If you do use this method, you still need to be aware of the almighty UIContext. Once again, when the event is triggered, the code handling the event is executed in the context that the event came in on (and you guess it, it is a different context than the UI is running in).

Well, depending on your code, this can be a lot easier to mitigate. If you are handling all your event handling in a different class than your UI class, then you will have to do the same as described above for SyncMessageHandler. However, if you are handling the event in your UI class, you don't need to worry about UIContext.BeginInvoke. In this case, the UIContext class has two methods that you can take full advantage of, Enter and Exit. Why don't you have to pass the UIContext around like before? Well, it is because one of the properties of an Avalon Windows Control is a UIContext property (called Context). So, in your code-behind where you capture the event, you can call Enter and Exit methods on the Context property of your Window.

Conclusion

I know I haven't discussed *much* here in this post, but I hope that it will save you from any future headaches when you start programming Indigo-enabled Longhorn applications.

Until next time, this has been Jason Olson and that's how the cheese melts. Ciao!

Posted on Monday, March 29, 2004 8:48 PM Windows Client | Back to top


Comments on this post: It's All About The Contexts

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Jason Olson | Powered by: GeeksWithBlogs.net