Wait for two events

Topics: Prism v4 - Silverlight 4
Feb 27, 2012 at 4:57 AM

Hi All,

I got 3 modules called A, B, C.

A fires an event x, B fires an event y.

C will be listened these two events x and y.

In case of event x => event handler called XEventHandler => Which will generate object A.

In case of event y => event handler called YEventHandler

In YEventHandler, Object A will be used. So I need some way to handle this so that:

1. If Object A is created => YEventHanlder will be executed if module C catch y Event

2. If Object A is not created yet (x event haven't been fired yet or the process of creating object A hasn't finished yet) => YEventHanlder need to wait until object A is ready and continue.

How can I solve this issue?

Thanks for such a cool community.

Truyenle

Developer
Feb 28, 2012 at 7:47 PM

Hi Truyenle,

If I understood you scenario correctly, a possible approach could be to implement a shared service. This service could:

  1. Contain a reference to Object A.
  2. Define an event (lets call it Event A), which could be raised when Object A is set/changed.

Following this approach, if XEventHandler is called, it should create Object A and set it in the shared service.

If YEventHandler is called, then it could check if Object A was set inside the shared service. If so, then the Object A is retrieved and the YEventHadler is executed correctly. If Object A was not set, then you could subscribe to Event A (which will be raised when Object A is set in the shared service) using another event handler or the same YEventHadler and return. When Event A is raised, you can retrieve Object A from the service and execute the corresponding logic in Module C.

However, as this is not strictly related to Prism, I believe you would find better support in the C# forums:

I hope you find this useful,

Damian Cherubini
http://blogs.southworks.net/dcherubini

Feb 29, 2012 at 2:10 PM

Thank you DCherubini, I'll give it a try and will update back.