How can I create events in a safer way?

Asked

Viewed 170 times

4

I am creating events according to the code below, however it seems to be a somewhat dangerous way, since if there is a cancellation of the event it will accumulate every "shot" of the event, that is, if it occurs three times in the fourth time it will run four times and not only one. How can I rewrite the code so that it cancels the event does not depend on a "=-" ?

 private static MainWindow _mainWindow;
    public static void Start(MainWindow mainWindow)
    {
        _mainWindow = mainWindow;
    }

    public static void AtivaProgressBar()
    {
        _mainWindow.ProgressBar1.IsIndeterminate = true;

        Suporte.Processo();

        Suporte.OnProcessoLongo += Suporte_OnProcessoLongo;
    }

    static void Suporte_OnProcessoLongo()
    {

        _mainWindow.StackPanel.Dispatcher.Invoke((Action)(() =>
        {
            _mainWindow.ProgressBar2.IsIndeterminate = true;

            MessageBox.Show("Call me");

            Suporte.OnProcessoLongo =- Suporte_OnProcessoLongo;

        }));
    }

public class Suporte
{
    public delegate void ProcessoLongo();

    public static event ProcessoLongo OnProcessoLongo;

    public static void Processo()
    {
        Task.Factory.StartNew((() => {

            Thread.Sleep(3000);

            RaiseProcessoLongo();
        }));
    }

    static void RaiseProcessoLongo()
    {
        if (OnProcessoLongo != null)
            OnProcessoLongo();
    }
}
  • That is, you want to remove all event subscriptions at once?

  • @Omni not necessarily, what I want is to make sure that once the event is signed, the developer doesn’t have to remove the subscription. Suppose a Framework contains a class that has events, the way it is whenever the developer signs an event he needs to remove the subscription, I wanted this removal to be done by the Framework itself and not by the Developer.

  • I was going to try to help but this code seems to have design problems. I mean, in the current form it’s either too broad or it’s unclear.

  • The correct is to put the event assignment += in a place that will only be called once (PAGE_LOAD?)

  • @Paulohdsousa but as it is an event, it can be triggered more than once. The idea is to do something similar to Uielemtent events that events (Gotfocus, Lostfocus, etc.) are "called" and do not require unsubscribe.

  • @julius_cesars the EVENT can be triggered more than once, his registration should not.

  • @julius_cesars Managed to solve?

Show 2 more comments

2 answers

3

The easiest way is to remove an event notifier from an event and then add it again so it won’t get duplicated. No problem removing the notification without having added before.

    public static void AtivaProgressBar()
    {
        _mainWindow.ProgressBar1.IsIndeterminate = true;

        Suporte.Processo();

        Suporte.OnProcessoLongo -= Suporte_OnProcessoLongo;
        Suporte.OnProcessoLongo += Suporte_OnProcessoLongo;
    }

0

DDD - Domain Events

There is a specific project pattern for this issue, called DDD - Domain Events.

This pattern has as its scope the layers communicate by events.

What is ?

"Domain Events, are events that make sense for the domain, that is, things that need to happen when a certain action occurs, for example, a transport system, we could have events like: "cargo delivered", "cargo in transport" or "cargo in separation".

And explaining the introduction a little bit, we say that domain events are a kind of message that describes something that happened in the past that is of business interest, we use that to separate all technical concerns from the domain, with this, your writing operations are all encapsulated in commands in the form of containers."

How to implement ?

Following tutorial

http://viniciusmussak.net/2015/08/30/domain-events/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.