Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Event Firing

Status
Not open for further replies.

tobriain

Programmer
Jul 19, 2006
13
IE
Hello,

I hope one of you have found a solution to the following problem before. . . Thanks in advance if you have.

I'm receiving events from an external dll (an RTDServer - set up like an observer pattern), and i've a Notify method that's called when there's an update to be collected. I can then collect a System.Array with a list of updates on a separate thread.

The problem is that the list will contain a number of different update types, and i've to set different variables in an object based on the differentupdates andI've to handle that list (one by one). Which of the following are possible/advisable, and can anyone recommend a better approach?

(a) use a hashtable/sortedlist/sorteddictionary with an indexer and the names of some variables in an object. Search the list, set the object variable, and let the object call any other events needed in the get/set method.

(b) write some kind of custom enumerator for the event types or variables to be set.

(c) use a hashtable/sortedlist/sorteddictionary with an indexer an objects of type Event. Just search the list for the indexer, and call the event to subscribers.

(d) some combination of the above.

The only thing is that speed is very important, because I migt have to call these events sequentially - I haven't investigated doing a foreach on the invocation list and calling asynchronously.Although they're small, I'll have possibly a hundred of these objects, with a hundred threads collecting updates.

Any help with this problem would be much appreciated.

Thanks,

Tom.


 
I'm confused,

You are receiving an array of objects in the same method that are to be used in different ways based on their type?

If this is the case then you want to set up a factory and some action classes. This should be quick and simple.


Step 1: Create an Interface that all of your action classes will use. A simple version is this:

Code:
public interface IActionClass
{
   void PopulateClass(object[] itemarray); //if you know the type you are passing in - like all strings then use string[] instead of object[]

   bool PerformAction();
}

Now we will create 2 classes that implement IActionClass

Code:
public class Car : IActionClass
{
     string brand;
     string model;

     public Car()
     {
     }

     public void PopulateClass(string[] itemarray) 
     {
         //item 0 is the defining ID - see the factory below
         brand = itemarray[1];
         model = itemarray[2];
     }

     public bool PerformAction()
     {
        Console.WriteLine("This car was best in class for 2006 : " + brand + " - " + model);

            return true;
     }
}


public class Speaker : IActionClass
{
     string serialnumber;

     public Speaker()
     {
     }

     public void PopulateClass(string[] itemarray) 
     {
         //Item 0 is the defining ID - see the factory below
         serialnumber = itemarray[1];
     }

     public bool PerformAction()
     {
        Console.WriteLine("This speaker has a serial number : " + serialnumber);

         return true;
     }
}


So Cars and Speakers are completely different... But your code doesn't care. Next you need to make a very primitive version of a factory.

Code:
public class ActionClassFactory
{
     public static IActionClass CreateActionClass(string[] items)
     {
          //use item 0 as the id. I would use a switch because I believe it is faster than ArrayLists and HashTables

          IActionClass ac = null;

          switch(items[0])
          {
              case "ISCAR":
              {
                  ac = new Car();
                  break;
              }
              case "ISSPEAKER":
              {
                  ac = new Speaker();
                  break;
              }
              case default:
              {
                   throw new Exception("Unknown ID!");
              }
          }

          ac.PopulateClass(items);

          return ac;
     }
}

And now inside your method that is the callback for the DLL you can do this:

Code:
private void mydll_Callback(string[] itemlist)
{
      IActionClass action = ActionClassFactory.CreateActionClass(itemlist);

      action.PerformAction();
}

To test this - simply make a test method

Code:
public void TestCallback()
{
      string[] carstring = new string[] {"ISCAR", "MAZDA", "RX8"};
      string[] speakerstring = new string[] {"ISSPEAKER", "123456ABC" };

     mydll_Callback(carstring);
     mydll_Callback(speakerstring);
}


Is that close to something you were looking for?

**Note : I did not test this code in a compiler - it's direct from my head so expect some errors...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top