Module dependency

Jul 25, 2009 at 6:53 PM

I have a dependecy issue in prism and I am not sure if there is a clean way to deal with this. In particular, my modules need to use some asynchronous events, but I don't see any "Loaded" events in Prism which would allow me to chain dependencies. Thus subsequent modules are loading and then running into uninitialized of null objects. Anyone have any experience with this?

 

Best,

RDL

Jul 27, 2009 at 6:01 PM

Hi RDL,

If I understand correctly you want to set up dependencies between modules so they are loaded in a particular order. I do not know what method you are using to load your modules, but all methods have a way to set up dependencies between modules. You can take a look at the Modularity Quickstart solution that is related to your approach to see how this is done.

The following are the how to topics of each module loading way, which explain how to set up dependencies between modules:

There is also a good post from Derek Greer that show Enhancing the Prism Module Initialization Lifecycle that might be useful in your scenario.

Please let me know if this helps.

Damian Schenkelman
http://blogs.southworks.net/dschenkelman

Sep 18, 2009 at 12:46 AM

Sorry, I didn't realize anyone responded to this and I deffered the problem to later.

It's not so much a particular order, but rather, I am making a bunch of asynchronous method calls in order to load initial data, and I need to wait until the asynchonous methods are completed before I go ahead. One thought was to use the event aggregator and create some a LoadedEvent, where the payload consits of the type that will be accessed through an instance of IUnityContainer, however, if the the instances I am waiting on are loaded before I subscribe to the LoadedEvent, then I will be hung. Of course, I could come up with another work around to that, but it seems like this is a pretty common problem and that there should be a relatively straightforward solution build into the framework?

Thanks

RDL

Sep 18, 2009 at 12:27 PM

I used a semaphore for a similiar requirement.  I had a spinner control that would spin whenever the semaphore was greater than 0, when it reached 0 it would stop.  Each async service was responsible for acquring/releasing on the semaphore (IUnityContainer singleton).  It raised events when it reached 0 and became non-zero - which the spinner subscribed to. 

Sep 18, 2009 at 2:01 PM

RDL - try something like this:

In your class define a private member:

private AutoResetEvent _wait;

Then in the method where you're initializing everything:

new Thread(new ThreadStart(delegate
{
	client.BeginWebServiceCall1(OnWebServiceCall1Complete);
	_wait.WaitOne();

	client.BeginWebServiceCall2(OnWebServiceCall2Complete);
	_wait.WaitOne();

	client.BeginWebServiceCall3(OnWebServiceCall3Complete);
	_wait.WaitOne();

	Continue();

})).Start();

And finally, make sure you have the callback methods to handle the completed web service calls as well as the method to continue processing:

private void OnWebServiceCall1Complete(object e)
{
	Deployment.Current.Dispatcher.BeginInvoke(delegate
	{
		// Do work here...
		_wait.Set();
	});
}

private void OnWebServiceCall2Complete(object e)
{
	Deployment.Current.Dispatcher.BeginInvoke(delegate
	{
		// Do work here...
		_wait.Set();
	});
}

private void OnWebServiceCall3Complete(object e)
{
	Deployment.Current.Dispatcher.BeginInvoke(delegate
	{
		// Do work here...
		_wait.Set();
	});
}

private void Continue()
{
	// Do whatever you would normally do now that everything is initialized.
}

 

 HTH

Ryan