Silveright Prism: How to wait for two events - silverlight-4.0

I am using Prism for my application and
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:
If Object A is created => YEventHanlder will be executed if module C catch y Event
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.

This seems to be a quite common task. Eventhandler Y has to wait for a WaitHandle, e.g. AutoResetEvent. This ResetEvent is set when object A is created.
private AutoResetEvent objectACreated = new AutoResetEvent(false);
private A a = null;
void HandlerY(object sender, EventArgs args)
{
if(objectACreated.WaitOne())
{
a.DoSomething();
}
}
void HandlerX(object sender, EventArgs args)
{
a = new A();
objectACreated.Set();
}

Related

How to get gtksharp refresh widget in event handler?

I am trying to get a label widget to update when a key is pressed. For some reason cannot get the label text to update, even though the Gtk thread correctly registers the key press (eg, writes the key to console). This is mono 4.4.0 and gtk-sharp 2.12 on macos.
public MainWindow() : base(Gtk.WindowType.Toplevel)
{
Build();
this.KeyPressEvent += ToddlerKeyPressEventHandler;
}
public void ToddlerKeyPressEventHandler (object o, Gtk.KeyPressEventArgs args) {
Gtk.Application.Invoke(delegate {
string letter = args.Event.Key.ToString();
this.letterLabel.Text = letter;
Console.WriteLine(letter);
this.letterLabel.QueueDraw();
});
}
I think you're missing an attribute. Try this:
[GLib.ConnectBeforeAttribute]
public void ToddlerKeyPressEventHandler (object o, Gtk.KeyPressEventArgs args)
{
letterLabel.Text = args.Event.Key.ToString ();
}
Bear in mind that if the event handler is hooked up to the label widget this won't work unless you also ensure that the "Selectable" property for that label widget is set to true (checked).

How to know when a project is finished opening

After initiating a new project, I would like to know when Petrel has completed the new project creation. When subscribed to the DataManager.WorkspaceOpened event, the event get called when the workspace is opened, but this may not be when the main thread has completed creating the new project.
Any ideas on when to know when Petrel is finished creating the new project ?
Perhaps you can try to find another way to solve your underlying problem than listening for the project event? Could you do a lazy evaluation and wait until the user actually need your code to do what it will do after the project is loaded?
Sometimes you might end up chasing these kind of problems in a circle: I need A to happen after B, and B to happen after C, and C to happen after A.
Anyway... here is a hack that you can try in you IModule:
void WorkspaceOpened(object sender, EventArgs e)
{
Application.Idle += new EventHandler(Application_Idle);
}
void Application_Idle(object sender, EventArgs e)
{
// Deatach from this, since this event can be raised 100 times per second,
// and we only want it once per project.
Application.Idle -= new EventHandler(Application_Idle);
DoMyStuffHereAfterTheProjectHasFinishedOpening();
}

Unsubscribe from IObservableElementEnumerable.EnumerableChanged doesn't work?

Parts of our UI uses IObservableElementEnumerable.EnumerableChanged in order to update if the user e.g. deletes a domain object from a folder.
When the UI is disposed, we unsubscribe from the event... or so we thought. It turns out that the unsubscribe doesn't have any effect, and our event handler is still called. This caused a number of odd bugs, but also leads to memory leaks.
The only time unsubscription works, is if we store the IObservableElementEnumerable reference instead of calling IObservableElementEnumerableFactory.GetEnumerable(obj) again. But this, in turn, is likely to keep a live reference to the folder object, which will break if the folder itself is deleted by the user.
This is particularly puzzling as the GetEnumerable() documentation clearly states: "It is expected that subsequent calls with the same domain object will yield the same instance of IObservableElementEnumerable." Is this not to be interpreted as a guarantee?
Should there be any reason for unsubscription not working?
The following code replicates the issue on Petrel 2011 (add to a simple plugin with a menu extension, or get the full solution here (DropBox)):
using System;
using System.Linq;
using System.Windows.Forms;
using Slb.Ocean.Core;
using Slb.Ocean.Petrel;
using Slb.Ocean.Petrel.Basics;
using Slb.Ocean.Petrel.UI;
namespace ObservableElementEnumerable
{
public class OEEForm : Form
{
private Droid _droid;
private bool _disposed;
public OEEForm()
{
IInput input = PetrelProject.Inputs;
IIdentifiable selected = input.GetSelected<object>().FirstOrDefault() as IIdentifiable;
if (selected == null)
{
PetrelLogger.InfoOutputWindow("Select a folder first");
return;
}
_droid = selected.Droid;
GetEnumerable().EnumerableChanged += enumerable_EnumerableChanged;
PetrelLogger.InfoOutputWindow("Enumerable subscribed");
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing && !_disposed)
{
GetEnumerable().EnumerableChanged -= enumerable_EnumerableChanged;
PetrelLogger.InfoOutputWindow("Enumerable unsubscribed (?)");
_droid = null;
_disposed = true;
}
}
IObservableElementEnumerable GetEnumerable()
{
if (_disposed)
throw new ObjectDisposedException("OEEForm");
object obj = DataManager.Resolve(_droid);
IObservableElementEnumerableFactory factory = CoreSystem.GetService<IObservableElementEnumerableFactory>(obj);
IObservableElementEnumerable enumerable = factory.GetEnumerable(obj);
return enumerable;
}
void enumerable_EnumerableChanged(object sender, ElementEnumerableChangeEventArgs e)
{
PetrelLogger.InfoOutputWindow("Enumerable changed");
if (_disposed)
PetrelLogger.InfoOutputWindow("... but I am disposed and unsubscribed!");
}
}
public static class Menu1
{
public static void OEEBegin1_ToolClick(object sender, System.EventArgs e)
{
OEEForm f = new OEEForm();
f.Show();
}
}
}
To replicate:
Run Petrel with the plugin
Load a project with a folder with objects
Select the folder
Activate the plugin menu item
With the popup open, delete an object in the folder
Close the Form popping up
Delete an object in the folder
The message log should clearly show that the event handler is still called after the form is disposed.
You already keep a reference to the underlying enumerable by connecting the event. Events are references as well. Just keep a reference to the enumerable and unsubscribe from the same instance as the one you subscribe to.
To deal with the issue of objects that are deleted by the user you need to listen to the delete event.

Calling multiple WCF service using IAsyncResult and AsyncCallback

I have one web page MyWebPage.aspx which while loading has to show data from two webservices along with it's own algorithm.
1) WebServiceI.SomeMethod() -> Takes 10 seconds aprx. to respond.
2) WebServiceII.SomeMethod() -> Takes 10 seconds aprx. to respond.
3) My Algorithm -> Takes 5 second aprx to respond.
Now,when I call this synchronously,this will take 10+10+5 = 25 seconds to load.
So,I was suggested "Asynchronous Calling Method",i.e. using IAsyncResult/AsyncCallback.
Now what will(should) happen is that all will be called simultaneously and the page will load in max 10 seconds.
So I call them now in the "Begin/End" way...
public partial class MyWebPage : System.Web.UI.Page
{
WebServiceI WebServiceIObject = new WebServiceI();
WebServiceII WebServiceIIObject = new WebServiceII();
protected void Page_Load(object sender, EventArgs e)
{
//BeginSomeMethod(AsyncCallback callback, object asyncState)[<- Method Signature]
WebServiceIObject.BeginSomeMethod(OnEndGetWebServiceISomeMethodResult, null);
//BeginSomeMethod(AsyncCallback callback, object asyncState)[<- Method Signature]
WebServiceIIObject.BeginSomeMethod(OnEndGetWebServiceIISomeMethodResult, null);
/* My Algorithm 5 seconds*/
DataSet DS = GetDataSetFromSomeWhere();
MyGataGrid.DataSource = DS.tables[0];
MyGataGrid.DataBind();
/* My Algorithm 5 seconds*/
//System.Threading.Thread.Sleep(6000);
}
//Will be called after 10 seconds
void OnEndGetWebServiceISomeMethodResult(IAsyncResult asyncResult)
{
string WebServiceISomeMethodResult = WebServiceIObject.EndSomeMethod(asyncResult);
MyLabelI.Text = WebServiceISomeMethodResult;
//EventLog MyLog = new EventLog("Application"); MyLog.Source = "MySourceI";
//MyLog.WriteEntry(DateTime.Now.ToString());
}
//Will be called after 10 seconds
void OnEndGetWebServiceIISomeMethodResult(IAsyncResult asyncResult)
{
string WebServiceIISomeMethodResult = WebServiceIIObject.EndSomeMethod(asyncResult);
MyLabelII.Text = WebServiceIISomeMethodResult;
//EventLog MyLog = new EventLog("Application"); MyLog.Source = "MySourceII";
//MyLog.WriteEntry(DateTime.Now.ToString());
}
}
Now the issue with the above example is that MyLabelI & MyLabelII Text are never set because the page loads after 5 seconds
and thread is released.Both End Methods are called correctly as checked by writing to EventLog.
How can I resolve this...
something like "All start at once and then all wait till all are complete..."
I understand that if my executing thread waits for 5 seconds more then the code executes as required..
How should I use AsyncWaitHandle...
Well,The answer to this problem is "System.Web.UI.PageAsyncTask" class.It allows Asynchronous calls to tasks and waits for
completion on the same thread.Also multiple tasks can be created and made to run parallel.Please go through the documentation
for further information...Will work in Asp.Net 2.0 & Above Only.
For our problem above...I am putting "My Algorithm" as sync and both other tasks as Async parallel.So my page will take 10+5
= 15 seconds to load.
public partial class MyWebPage : System.Web.UI.Page
{
WebServiceI WebServiceIObject = new WebServiceI();
WebServiceII WebServiceIIObject = new WebServiceII();
protected void Page_Load(object sender, EventArgs e)
{
PageAsyncTask PAT_I = new PageAsyncTask
(BeginGetWebServiceISomeMethodResult, OnEndGetWebServiceISomeMethodResult, null, null, true);
Page.RegisterAsyncTask(PAT_I);
PageAsyncTask PAT_II = new PageAsyncTask
(BeginGetWebServiceIISomeMethodResult, OnEndGetWebServiceIISomeMethodResult, null, null, true);
Page.RegisterAsyncTask(PAT_II);
Page.ExecuteRegisteredAsyncTasks();
/* My Algorithm 5 seconds*/
DataSet DS = GetDataSetFromSomeWhere();
MyGataGrid.DataSource = DS.tables[0];
MyGataGrid.DataBind();
/* My Algorithm 5 seconds*/
}
IAsyncResult BeginGetWebServiceISomeMethodResult
(object Sender, EventArgs EventArgsObject,
AsyncCallback AsyncCallbackObject, object PassAnythingExtraIfRequired)
{
return WebServiceIObject.BeginSomeMethod(AsyncCallbackObject, PassAnythingExtraIfRequired);
}
IAsyncResult BeginGetWebServiceIISomeMethodResult
(object Sender, EventArgs EventArgsObject,
AsyncCallback AsyncCallbackObject, object PassAnythingExtraIfRequired)
{
return WebServiceIIObject.BeginSomeMethod(AsyncCallbackObject, PassAnythingExtraIfRequired);
}
void OnEndGetWebServiceISomeMethodResult(IAsyncResult asyncResult)
{
string WebServiceISomeMethodResult = WebServiceIObject.EndSomeMethod(asyncResult);
MyLabelI.Text = WebServiceISomeMethodResult;
}
void OnEndGetWebServiceIISomeMethodResult(IAsyncResult asyncResult)
{
string WebServiceIISomeMethodResult = WebServiceIIObject.EndSomeMethod(asyncResult);
MyLabelII.Text = WebServiceIISomeMethodResult;
}
}
Works :) The code can be made generic with common refactoring...
Please be careful while planning for this kind of design though...Internally this must also be using threads from threadpool
only...I have not dug in deep..but must be..So if the tasks happen to take moretime than planned and if several such tasks
get raised at the same time,then web server could suffer and users could get time out...
I am still carrying this forward inspite of above flaw as my users will time out anyway if 25 seconds turns out to be 55
seconds...Better to have a situation where some users are able to work rather than none..
If there is some better alternative,please post.

Removing all event handlers in one go

Problem: I have a document class which contains a list of objects. These objects raise events such as SolutionExpired, DisplayExpired etc. The document needs to respond to this.
Documents can sometimes exchange objects, but a single object should never be 'part' of more than one document.
My document class contains a bunch of methods which serve as event handlers. Whenever an object enters the document, I use AddHandler to set up the events, and whenever an object is removed from the document I use RemoveHandler to undo the damage. However, there are cases where it's difficult to make sure all the steps are properly taken and I might thus end up with rogue event handlers.
Long story short; how do I remove all the handlers that are pointing to a specific method? Note, I don't have a list of potential event sources, these could be stored anywhere.
Something like:
RemoveHandler *.SolutionExpired, AddressOf DefObj_SolutionExpired
You can use Delegate.RemoveAll(). (The part you're interested in is in button2_Click)
public void Form_Load(object sender, EventArgs e)
{
button1.Click += new EventHandler(button1_Click);
button1.Click += new EventHandler(button1_Click);
button2.Click += new EventHandler(button2_Click);
TestEvent += new EventHandler(Form_TestEvent);
}
event EventHandler TestEvent;
void OnTestEvent(EventArgs e)
{
if (TestEvent != null)
TestEvent(this, e);
}
void Form_TestEvent(object sender, EventArgs e)
{
MessageBox.Show("TestEvent fired");
}
void button2_Click(object sender, EventArgs e)
{
Delegate d = TestEvent as Delegate;
TestEvent = Delegate.RemoveAll(d, d) as EventHandler;
}
void button1_Click(object sender, EventArgs e)
{
OnTestEvent(EventArgs.Empty);
}
You should note that it doesn't alter the contents of the delegates you pass in to it, it returns an altered delegate. Consequently, you won't be able to alter the events on a button you've dropped on a form from the form, as button1.Click can only have += or -= used on it, not =. This won't compile:
button1.Click = Delegate.RemoveAll(d, d) as EventHandler;
Also, be sure that wherever you're implementing this you're watching out for the potential of race conditions. You could end up with some really strange behavior if you're removing handlers from an event that is being called by another thread!
public class TheAnswer
{
public event EventHandler MyEvent = delegate { };
public void RemoveFromMyEvent(string methodName)
{
foreach (var handler in MyEvent.GetInvocationList())
{
if (handler.Method.Name == methodName)
{
MyEvent -= (EventHandler)handler;
}
}
}
}
EDIT 2: Apologies for my misunderstanding--I see that you were pretty clear about not having access to the event sources in your original post.
The simplest way I can think of to solve this problem involves implementing a Shared dictionary of object-to-document bindings. When an object enters a document, check the dictionary for an existing binding to another document; if present, remove handlers that refer to the old document before adding them for the new. Either way, update the dictionary with the new binding.
I think in most cases the performance and memory impacts would be negligible: unless you're dealing with many tens of thousands of small objects and frequently exchange them between documents, the memory overhead of each key/value pair and performance hit for each lookup operation should be fairly small.
As an alternative: if you can detect (in the document event handlers) that the sender of the event is no longer relevant to the document, you can detach the events there.
These seem like the kind of ideas you might have already rejected--but maybe not!
Use Delegate.RemoveAll (maybe using reflection if the Delegate instance is private).