• (2 Pages)
  • +
  • 1
  • 2

Quick and easy custom events Demonstrated with simple event-triggered logging

#16 tlhIn`toq  Icon User is offline

  • Freelance developer
  • member icon

Reputation: 6252
  • View blog
  • Posts: 13,797
  • Joined: 02-June 10

Posted 15 April 2011 - 10:01 AM

This can be somewhat misleading since all of your test application is taking place in the same thread.

The subscriptions happen in order. When an event gets raised notification happens in that same order. The Console takes commands in order.

If you have event subscriptions that take advantage of parrallel processing in different threads and even on different cores of a multi-core PC you would see dramatic time savings in real-world code where each thread is actually performing some real processing and not just outputting to the console which is a bit of a bottleneck.
Was This Post Helpful? 1
  • +
  • -

#17 here.to.code  Icon User is offline

  • D.I.C Head

Reputation: 20
  • View blog
  • Posts: 55
  • Joined: 15-February 11

Posted 15 April 2011 - 12:36 PM

The test is a valid for testing your statement that was pinned against your method vs calling of a function that does the same thing. All I'm validating is that your method is also sequential.
Also this has nothing to do with multi-threading or parallel programming either way. Like I said it was just to test your statement. Which is false.
Was This Post Helpful? 0
  • +
  • -

#18 tlhIn`toq  Icon User is offline

  • Freelance developer
  • member icon

Reputation: 6252
  • View blog
  • Posts: 13,797
  • Joined: 02-June 10

Posted 15 April 2011 - 01:41 PM

When I say 'sequential' I mean that the first item has to finish completely before the second item can begin. That at no time are the two methods happening at the same time (concurrently). You can start two methods in sequence same time, have each require several seconds to complete then finish in a different order. Just because they started A, B, C doesn't mean they will finish A, B, C.

With small, short methods on a single thread targeting into the console (itself a sequential output) you just can't see the difference between direct calling of methods and events activating other components and more involved processes.

If you feel you don't need to use events and that you can accomplish everything you want by just calling the methods directly then please feel free to pursue your style of coding. Coding does indeed require and create a personal style and that is one of the things I love: There is no single, one right way to do something. Ten coders will find ten ways to write the same program and every one of them will have enough distinct stylistic differences to be able to identify the coder. Its like identifying an artist by looking at the style and brushstrokes even if they didn't sign the painting.
Was This Post Helpful? 2
  • +
  • -

#19 Mallaboro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 26-April 10

Posted 10 August 2012 - 07:50 PM

Thanks again for your help. This guide was just what I was after. :)
Was This Post Helpful? 0
  • +
  • -

#20 stixoffire  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 05-August 15

Posted 05 August 2015 - 12:09 PM

View PostRico Diesel, on 08 June 2010 - 08:11 AM, said:

Nice article, it has all the information necessary for custom events (at least in my humble opinion), but I got a question though. Why would one use an extra private variable to raise the event?

public event EventHandler<TextArgs> Feedback;

private void RaiseFeedback(string p)
{
  EventHandler<TextArgs> handler = Feedback;
  if (handler != null)
  {
    handler(null, new TextArgs(p));
  }
}



Why not call it like this?
if (Feedback != null)
  Feedback(null, new TextArgs(p));



I have seen this now quite a few times on several different fora and articles but I couldn't find an explanation for this so far. Most programmers always seem to use the extra variable 'handler'. As far as I can tell it seems to be a preference, instead of being really functional, but I could be wrong of course...

Rico



Try a Multi threaded application and you will know why in a hurry.
The new instance of the handler is being created so you will not end up with a race condition .
When you do multi threading you declare a lock object for threaded apps, and on a property this public string myProperty {get; set;}
is different than public string myProperty ;
The first is thread safe the second is not. The first the compiler automatically creates the lock on the property for you , in the second you would need to do it manually .
So in short if you follow the proper example of doing it - you will be able to transition to a thread safe application much easier , if you do it the right way all of the time you will eliminate issues in your future coding.

This post has been edited by tlhIn`toq: 05 August 2015 - 12:39 PM

Was This Post Helpful? 1
  • +
  • -

#21 PierreDupont  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-February 16

Posted 26 February 2016 - 10:24 AM

Hello all,

Nice to find this forum and this topic.
This is especially usefull in multiForms applications needing to be refreshed accordingly to what was done from one of them.

Here is the way I used it:

Declaration in main form (Form1)
        public event EventHandler<EventArgs> ActualiseF;
        private void RaiseActualiseF()
        {
            EventHandler<EventArgs> handler = ActualiseF;
            if (handler != null)
            {
                handler(null, new EventArgs());
            }
        }



All child forms are created this way:
        private void ouvreGestResa()
        {
            if (Application.OpenForms["FormReservations"] == null)
            {
                FormReservations formResa = new FormReservations(this);
                formResa.Text = titreFormResa;
                ActualiseF += new EventHandler<EventArgs>(formResa.actuFormResa);
                formResa.Show();
            }
            else
            {
                FormReservations fRes = Application.OpenForms["FormReservations"] as FormReservations;
                fRes.BringToFront();
            }
        }



On line 07 you can see that when the event ActualiseF will be raised, formResa will start the prrocedure "actuFormResa". this will reload the udated bookings data and reload all buttons and label if, from Form1, the user has selected another language.
        public void actuFormResa(object sender, EventArgs e)
        {
            actuResaEnCours();
            afficheLibelles();
        }



That way, each time the opened forms need to be updated, instead of all that littérature checking for all opened forms
a simple "RaiseActualiseF();" does the job :bigsmile:/>
/*
                        if (Application.OpenForms["FormReservations"] != null)
                        {
                            //FormReservations fr = new FormReservations(this);
                            FormReservations fr = Application.OpenForms["FormReservations"] as FormReservations;
                            fr.afficheLibelles();
                        }
                        if (Application.OpenForms["FormClavierTactile"] != null)
                        {
                            FormClavierTactile fctt = Application.OpenForms["FormClavierTactile"] as FormClavierTactile;
                            fctt.afficheLibelles();
                        }
                        if (Application.OpenForms["FormLSCasiers"] != null)
                        {
                            FormLSCasiers fca = Application.OpenForms["FormLSCasiers"] as FormLSCasiers;
                            fca.actuCasiersLS();
                        }
                        if (Application.OpenForms["FormLSElements"] != null)
                        {
                            FormLSElements fel = Application.OpenForms["FormLSElements"] as FormLSElements;
                            fel.actuElementsLS();
                        }
                        if (Application.OpenForms["FormLSEmprunts"] != null)
                        {
                            FormLSEmprunts fem = Application.OpenForms["FormLSEmprunts"] as FormLSEmprunts;
                            fem.actuEmpruntsLS();
                        }
                         * */
                        /*
                        if (Application.OpenForms["FormClavierTactile"] != null)
                        {
                            FormClavierTactile fctt = Application.OpenForms["FormClavierTactile"] as FormClavierTactile;
                            fctt.afficheLibelles();
                        }
                         */
                        RaiseActualiseF();



Big thanks to all for these helpfull tips

Pierre
Was This Post Helpful? 0
  • +
  • -

#22 PierreDupont  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 26-February 16

Posted 29 February 2016 - 03:38 AM

OOOPS ... TAKE CARE : IMPORTANT UPDATE FOR MY PREVIOUS POST :hammer:/>/>

I noticed just now that when a child form has been closed the event still triggers its procedure which, off cours raises an exception as the initialisations like datagrid bindings are not done ...

To avoisd this, when closing the child form, DON'T FORGET to unsubscribe the procedure from the event :tooth:/>/>

So, when the user opens the formResa, the procedure actuFormResa is subscribed to the event ActualiseF

        private void ouvreGestResa()
        {
            if (Application.OpenForms["FormReservations"] == null)
            {
                FormReservations formResa = new FormReservations(this);
                formResa.Text = titreFormResa;
                ActualiseF += new EventHandler<EventArgs>(formResa.actuFormResa);
                formResa.Show();
            }
            else
            {
                FormReservations fRes = Application.OpenForms["FormReservations"] as FormReservations;
                fRes.BringToFront();
            }
        }



When the user closes formResa, we must unsubscribe the procedure actuFormResa from ActualiseF

        private void FormReservations_FormClosed_1(object sender, FormClosedEventArgs e)
        {
            formPri.ActualiseF -= new EventHandler<EventArgs>(actuFormResa);
        }



NB formPri is the name given to the mainForm which called formResa
public FormReservations(Form formP)
        {
            InitializeComponent();
            formPri = formP as Form1;


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2