15 Replies Latest reply on Sep 30, 2009 7:05 AM by fkeereweer

    EventPipe Events Example - C#

    fkeereweer

      Hello,

       

      I tried to do the "EventPipe Events Example" for C# delivered with the About PI-SDK helpfile. Unfortunately I got the error below here:

       

      Unable to cast COM object of type 'System.__ComObject' to interface type 'PISDK._EventPipeEvents'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{9E679FD4-DE8C-11D3-853F-00C04F45D1DA}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

       

      This error occurs in next code:

       

      mdbEvents = srv.PIModuleDB.EventPipe; 
      // get MDB events pipe

       

      I hope you can help me. Thanks in advance!

       

      Bye,

       

      Frits

        • Re: EventPipe Events Example - C#
          andreas

          Hi,

           

          strange - the code works for me (Visual Studio 2008). Is your default server the vCampus PI or is there any chance that the default server that is used in the example is a special one (no MDB for example)?

           

          regards,

            • Re: EventPipe Events Example - C#
              fkeereweer

               

               

              Hello,

               

              I uses also Visual Studio 2008. I connect to my local PI server, which is installed with the installation files from the vCampus (is that what you mean?). This PI server has an MDB. But with an EventPipe for PIPoints the same problem occurs.

               

              Best regards,

               

              Frits

               

               

                • Re: EventPipe Events Example - C#
                  andreas

                  Frits,

                   

                  I connect to my local PI server, which is installed with the installation files from the vCampus (is that what you mean?)

                   

                  yes. But it get's more confusing now. Could you post the complete code?

                   

                  Here is my code, the necessary reference for this is OSIsoft.PISDK:

                  using System;
                  using System.Collections.Generic;
                  using System.ComponentModel;
                  using System.Data;
                  using System.Drawing;
                  using System.Linq;
                  using System.Text;
                  using System.Windows.Forms;
                  using PISDK;
                  using System.Threading;
                  using System.Runtime.InteropServices;

                  namespace PISDK_EventPipeExample
                  {

                      publicpartialclassForm1 : Form
                      {

                          privatePISDKClass piSDK = newPISDKClass();
                          privateServer srv;
                          private PISDK.EventPipe mdbEvents; // use ModuleDB events
                          //-----------------------------------------------------------------
                          // This is the event interface (interop).
                          // All similar events have their own "eventname_Event" interface that was created by
                          // the typelib exporter and available in PISDK interop assembly. This is the recommended
                          // way to utilize COM events from EventPipes (the others are much harder)
                          private PISDK._DEventPipeEvents_Event epEvents;
                          int evCnt = 0;

                          public Form1()
                          {
                              System.Threading.Thread.CurrentThread.SetApartmentState(ApartmentState.STA);

                              //
                              // Required for Windows Form Designer support
                              //
                              InitializeComponent();
                              srv = piSDK.Servers.DefaultServer;

                          }

                          privatevoid Form1_Load(object sender, EventArgs e)
                          {

                              try
                              {
                                  mdbEvents = srv.PIModuleDB.EventPipe; // get MDB events pipe
                                  epEvents = (_DEventPipeEvents_Event)mdbEvents; // get events interface (special interop)
                                  epEvents.OnNewValue += new_DEventPipeEvents_OnNewValueEventHandler(epEvents_OnNewValue); // subscribe
                              }
                              catch (Exception ex)
                              {
                                  MessageBox.Show(this, "error:" + ex.Message + " Event interface failed");
                              }
                          }

                          // New value event handler - one or more MDB objects has been edited/added/deleted
                          // just count the events
                          privatevoid epEvents_OnNewValue()
                          {

                              evCnt += mdbEvents.Count; // add events
                              mdbEvents.TakeAll(); // take the events once the counter has been incremented
                              MessageBox.Show(evCnt.ToString());

                          }
                      }
                  }

                  regards,

                    • Re: EventPipe Events Example - C#
                      fkeereweer

                       Hello Andreas,

                       

                      My code is moreless te same as your code (see below here). I uses the references OSIsoft.PISDK and OSIsoft.PISDKCommon. Furthermore I added a button to test the eventpipe for PIPoints.

                      using System;
                      using System.Collections.Generic;
                      using System.ComponentModel;
                      using System.Data;
                      using System.Drawing;
                      using System.Linq;
                      using System.Text;
                      using System.Windows.Forms;
                      using PISDK;
                      using System.Threading;
                      using System.Runtime.InteropServices;


                      namespace PI_EventPipe
                      {
                          public partial class Form1 : Form
                          {
                              public PISDKClass piSDK = new PISDKClass();
                              public Server srv;
                              public PISDK.EventPipe mdbEvents; // use ModuleDB events
                              public System.Windows.Forms.Label label1;
                              public System.Windows.Forms.TextBox textBox1;
                              //-----------------------------------------------------------------
                              // This is the event interface (interop).
                              // All similar events have their own "eventname_Event" interface that was created by
                              // the typelib exporter and available in PISDK interop assembly. This is the recommended
                              // way to utilize COM events from EventPipes (the others are much harder)
                              public PISDK._DEventPipeEvents_Event epEvents;
                              public System.Windows.Forms.Label label2;
                              public int evCnt = 0;


                              public Form1()
                              {
                                  //System.Threading.Thread.CurrentThread.ApartmentState = ApartmentState.STA;
                                  System.Threading.Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
                                  //
                                  // Required for Windows Form Designer support
                                  //
                                  InitializeComponent();
                                  srv = piSDK.Servers["MNLD073"];

                              }

                              private void Form1_Load(object sender, EventArgs e)
                              {
                                 
                                  try
                                  {
                                      mdbEvents = srv.PIModuleDB.EventPipe; // get MDB events pipe
                                      epEvents = (_DEventPipeEvents_Event)mdbEvents; // get events interface (special interop)
                                      epEvents.OnNewValue += new _DEventPipeEvents_OnNewValueEventHandler(epEvents_OnNewValue); // subscribe
                                  }
                                  catch (Exception ex)
                                  {
                                      MessageBox.Show(this, "error:" + ex.Message + " Event interface failed");
                                  }
                                 
                              }

                              // New value event handler - one or more MDB objects has been edited/added/deleted
                              // just count the events

                              private void epEvents_OnNewValue()
                              {
                                  evCnt += mdbEvents.Count; // add events
                                  mdbEvents.TakeAll(); // take the events once the counter has been incremented
                                  textBox1.Text = evCnt.ToString();
                              }

                              private void button1_Click(object sender, EventArgs e)
                              {

                                  PISDK.PIPoint pt;
                                  PISDK.EventPipe evp1;

                                  pt = srv.PIPoints["Sinusoid"];
                                  evp1 = (PISDK.EventPipe)pt.Data.EventPipe;
                                  evp1.MaxCount = 10;
                                                
                              }
                          }
                      }

                      Best regards,

                       

                      Frits