ICommentEvent runs TWICE in ONE comment event Sitefinity - comments

I have these code, according to document from sitefinity:
protected void Application_Start(object sender, EventArgs e)
{
Bootstrapper.Initialized += new EventHandler<ExecutedEventArgs>(Bootstrapper_Initialized);
}
public void Bootstrapper_Initialized(object sender, ExecutedEventArgs args)
{
if (args.CommandName == "Bootstrapped")
{
EventHub.Subscribe<ICommentEvent>(evt => CommentsEvent.CommentEventHandler(evt));
}
}
And the handler:
public static void CommentEventHandler(ICommentEvent evt)
{
// My code here
}
The problem is this handler always runs twice when a comment event happens (post a comment or approve a comment).
Could you please tell me why this happens and any possible way to avoid this? (I don't believe static boolean is a good idea).
Thanks

ICommentEvent is a base interface that is implemented by multiple events such as ICommentCreatingEvent, ICommentCreatedEvent, ICommentUpdatingEvent, ICommentUpdatedEvent and some others.
In your case it is fired twice due to firing of both ICommentCreatingEvent and ICommentCreatedEvent.
You can subscribe to just one of them and it should fire just once.

Related

Using Events From A Class

I am trying to get a function to be called everytime an event occurs. In the KinectRegion class there is an event called HandPointerGrip: http://msdn.microsoft.com/en-us/library/microsoft.kinect.toolkit.controls.kinectregion.handpointergrip.aspx.
I see that it has declared the event and it seems to me that the event has already been set to be invoked(HandPointerEventArgs)? How do I attach a function to this event?
public Menu()
{
KinectRegion.HandPointerGripEvent+=Hand_Gripped; // why doesn't this work? :(
}
private void Hand_Gripped(object sender, HandPointerEvnetArgs e)
{
MessageBox.Show("I work!"); // I wish this would work
}
Been working hard on this problem and here is something I think will work. Afraid to test it. Learning a lot about routed events, delegates, and events.
namespace ...
{
public delegate void HandPointerEventHandler(object sender, HandPointerEventArgs e);
public partial class thePage : Page
{
public event HandPointerEventHandler HandGripped
{
add {this.AddHandler(KinectRegion.HandPointerGripEvent,value);}
remove {this.RemoveHandler(KinectRegion.HandPointerGripEvent,vlaue);}
}
public thePage()
{
InitializeComponent();
this.HandGripped += new HandPointerEventHandler(OnHandGripped);
}
protected virtual void OnHandGripped(object sender, HandPointerEventArgs e)
{
MessageBox.Show("hello"); //hopefully
}
}
}
The first block of code should work fine. My guess is that the HandPointerGripEvent is hooked up ok, it just never fires.
How are you setting up your KinectRegion?
Are you updating the interration library each frame?
Perhaps this helps?
Kinect SDK 1.7: Mapping Joint/Cursor Coordinates to screen Resolution
KinectRegion.AddHandPointerGripHandler(this.Button1, this.Button1_Click);
Here Button1 is:
< k:KinectTileButton x:Name="Button1" Height="150" Width="150" Content="Click"/ >
The namespaces:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:k="http://schemas.microsoft.com/kinect/2013"
Button1_Click is the method itself, for example:
private void Button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("YOU GOT ME !!!");
}
If you want to add a grip handler for another interface object, you just do:
KinectRegion.AddHandPointerGripHandler(< object name >, < method name >);
And s.o.

GWTP: event sent once but received (handler) twice

On GWTP I am sending a UpdateDiagramBoxEvent with the code below, but the handler is executed twice. In other words, I can see that the sendUpdateDiagramBoxEvent() is executed only once, but it is received twice. The same is happening with many other events on my code. Any ideas of what is wrong, and how can I avoid this behaviour? THANKS.
Receive event
UpdateDiagramBoxHandler updateDiagramBoxHandler = new UpdateDiagramBoxHandler() {
#Override
public void onUpdateDiagramBox(UpdateDiagramBoxEvent event) {
doSomething();
}
};
Send event
EventUtil.sendUpdateDiagramBoxEvent(CellTableManager.this.eventBus,
BasicConstants.EventSubscriptors.VIEW, 0,
BasicConstants.EditableTableFields.DIAGRAMTYPE,
ClientState.getCurrentDiagramType().name());
public static void sendUpdateDiagramBoxEvent(final EventBus eventBus,
final BasicConstants.EventSubscriptors recipient,
final int index, final BasicConstants.EditableTableFields field,
final String value){
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
#Override
public void execute() {
UpdateDiagramBoxEvent updateDiagramBoxEvent =
new UpdateDiagramBoxEvent(transactionNumber, recipient,
field.toString(), value, index);
eventBus.fireEvent(updateDiagramBoxEvent);
}
});
}
Register event handler (from MyProjectPresenter.java)
#Inject PlaceManager placeManager;
#Override
protected void onBind() {
[...]
registerHandler(getEventBus().addHandler(UpdateDiagramBoxEvent.getType(),
updateDiagramBoxHandler));
}
It generally means that you simply registered your event handlers twice.
Is this GWTP and if so how are you registering your events/handlers? I seem to recall there is a pitfall that you can use either #ProxyEvent or addRegisteredHandler() but not both, or you will receive the events twice.
Hope that helps.
Cheers,
Or the bean in question might not be singleton.

Eventhandler is fired more than once

I have a problem with two of my EventHandlers, they work the same, so here is one:
private void Form1_Load(object sender, EventArgs e)
{
webBrowserWebsite.Url = new System.Uri(textBoxURL.Text, System.UriKind.Absolute);
webBrowserWebsite.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowserWebsite_DocumentCompleted);
}
void webBrowserWebsite_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\CurrentData.wfd");
sw.Write(webBrowserWebsite.Document.Body.InnerText);
sw.Close();
}
The problem is, that the EventHandler fires multiple times, it doesn't stop!
Why is it doing this?
Thanks in advance
The code that you have written won't compile (your StreamWriter in your EventHandler isn't assigned to anything) and without more context as to how you are calling this, it is difficult to say for certain.
But the most likely reason is you are calling Form1_Load multiple times, but using the same webBrowserWebsite object. Each time the form loads, you are adding a new Event Handler. And since you aren't showing any code showing where you removing the event handler, I'm guessing it fires once for each time you call Form_Load.
Depending on your design, you are better off adding the event handler in the constructor so it is only added once regardless of the number of times you load the form.
public Form1()
{
webBrowserWebsite.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowserWebsite_DocumentCompleted);
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowserWebsite.Url = new System.Uri(textBoxURL.Text, System.UriKind.Absolute);
}
Or remove the event handler in the event handler:
void webBrowserWebsite_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\CurrentData.wfd");
sw.Write(webBrowserWebsite.Document.Body.InnerText);
sw.Close();
webBrowserWebsite.DocumentCompleted -= webBrowserWebsite_DocumentCompleted;
}
Also, since StreamWriter implements IDisposible, you should be putting it inside of a using block or at least calling sw.Dispose() at the end of the method

Live SDK Windows Phone 7 GetCompleted callback in UI thread?

public class SyncHelper
{
private LiveConnectClient client;
public event EventHandler SyncStarted;
public event EventHandler SyncCompleted;
public SyncHelper(LiveConnectClient client)
{
this.client = client;
}
public void TrySync()
{
Debug.WriteLine("Beginning sync");
OnSyncStarted();
client.GetCompleted += OnGetCompleted;
client.GetAsync("me/skydrive/files");
}
private void OnGetCompleted(object sender, LiveOperationCompletedEventArgs e)
{
Thread.Sleep(10000);
Debug.WriteLine("Get Completed");
client.GetCompleted -= OnGetCompleted;
OnSyncCompleted();
Debug.WriteLine("Sync completed");
}
private void OnSyncStarted()
{
if (SyncStarted != null)
SyncStarted(this, new EventArgs());
}
private void OnSyncCompleted()
{
if (SyncCompleted != null)
SyncCompleted(this, new EventArgs());
}
}
The function OnGetCompleted is being called in the UI thread and the UI is unresponsive. From whatever I know, I thought these callbacks would work in a different thread and we would have to use the displatcher to post it to the UI thread. Any thoughts? Help!
The GetAsync call is likely using a background thread to do the actual fetch, but then it's trying to help you by calling the Completed callback in the original thread context so you don't have to use a Dispatcher.
Why are you putting in a Sleep(10000) anyway? The callback says "hey, I'm done". At that point you should update the UI if you want. If you need to do further processing that takes significant time, spawn a background thread, threadpool task or use another asynchronous call with another callback.

Handling multiple events from a service on a controller

Say I have the controller as follows:
public class Controller
{
ISomeService _service;
public Controller(ISomeService service)
{
_service = service;
_service.EventFired += EventFired;
_service.SomeEventFired += SomeOtherEventFired;
}
private void EventFire(object sender, EventArgs e)
{
// Might occur on FireSomeEvents();
// Go to another controller
}
private void SomeOtherEventFired(object sender, EventArgs e)
{
// Might occur on FireSomeEvents();
// Go to another view on this page
}
public void Create()
{
_service.FireSomeEvents();
if(EventFired == true)
{
return View("EventFired");
}
else
{
return RedirectToAction("SomeOtherEventFired");
}
}
}
I want to be able to handle the Redirect and View in a better way, because in the end I will end up with 3 potential events on my service.
I'm just wondering whether this is a design smell, or whether there is a better way to implement the redirect to pages...
Events don't play nicely with ASP.NET MVC. Their model is not adapted to the MVC pattern. If you want to use them you may take a look at asynchronous controllers.

Resources