Event Queue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tb2500
    New Member
    • Aug 2009
    • 6

    Event Queue

    Dear all,

    I want to create a Queue to add events that have been raised to, so later these events can be popped off the Queue stack and an appropriate Event Handler can be assigned. I wondered if this is actually possible?

    How do I add events to a Queue?

    So here's how it should go:

    1. Raise an event ( no event handler for it exists yet )
    2. Add this raised event to a Queue ( or a reference to it )
    3. Sometime later... Take event off Queue and assign handler
    4. Execute Handler

    Hope somebody can help?
  • cloud255
    Recognized Expert Contributor
    • Jun 2008
    • 427

    #2
    Originally posted by tb2500
    Dear all,
    So here's how it should go:

    1. Raise an event ( no event handler for it exists yet )
    2. Add this raised event to a Queue ( or a reference to it )
    3. Sometime later... Take event off Queue and assign handler
    4. Execute Handler
    I don't think this is the correct order of operation with which to approach this situation.

    I would suggest that you create a class which exposes a delegate and a raise() method.

    You need to at some point pass a function to the delegate within your class. I would suggest you use a List instead of a queue as this would make access to any given element of the List far neater.

    Next you can create a list of your class type eg:
    Code:
    List<MyClass> EventsList = new List<MyClass>();
    You can then add instances of your class to the list:

    Code:
    MyClass customEvent = new MyClass();
    EventsList .Add(customEvent );
    You may then add pass the event handler to the delegate of your class:

    Code:
    EventsList [index].MyEvent += new MyClass.MyDelegate(EventHandler);
    Later on you can then raise the event and remove it from the list:

    Code:
    EventsList [index].Raise();
    EventsList.Remove(customEvent);
    If I may ask, why do you want to delay the execution of events?

    Comment

    • tb2500
      New Member
      • Aug 2009
      • 6

      #3
      Hi,

      To answer your question...
      I have a program with 2 Forms. Form A is active when program starts and does some work, then after user clicking Button, Form B becomes active and Form A Closes down. I want to pass data from form A to Form B through an event, but since Form B doesn't exist at the time the data in Form A is created, I need to delay the handling of the event.

      Unless you have another way of sharing data between two classes?

      Thanks again for your help.

      Comment

      • cloud255
        Recognized Expert Contributor
        • Jun 2008
        • 427

        #4
        Hi,

        Well sure you create a class which will hold all the required data. Create and instance of that class and populate it in form 1.

        Then you simply pass that instance to form 2 by either exposing a property in form 2 or by adding a parameter of the class type to form2's constructor...

        Code:
        private MyDataHolderClass copyOfForm1Data = new  MyDataHolderClass ();
        
        public frmSecondForm(MyDataHolderClass dataFromForm1)
                {
                    InitializeComponent();
                    copyOfForm1Data = dataFromForm1;
                }
        and then frmSecondForm has all the data captured in form1.

        Comment

        • tb2500
          New Member
          • Aug 2009
          • 6

          #5
          Hi,

          Thanks for that! :) That was very helpful. I think I was trying to make it too complicated.

          Comment

          Working...