YetAnotherForum
Welcome Guest Search | Active Topics | Log In | Register

How to handle OnDisconnect in C# Options · View
jfaigan
#1 Posted : Wednesday, August 26, 2009 8:33:18 PM
Rank: Member
Groups: Member

Joined: 8/26/2009
Posts: 10
Location: Canada
I am trying to subscribe to the OnDisconnect event of the Server object but I am getting an invalid cast exception.

Here is what I am doing.

Can you tell me what is going wrong? Any help would be most appreciated!

Regards,
Joel.

PISDK.PISDK _piSDK = new PISDK.PISDK();

Server _piSDKServer = _piSDK.Servers["server_name"];

IServerDisconnect _srvdis = (IServerDisconnect) _piSDKServer; //The error occurs here. Invalid cast error on DisconnecEvents.

ServerDisconnectEvents srvdisevt = _srvdis.DisconnectEvents;

srvdisevt.OnDisconnect += srvdisevt_OnDisconnect;


void srvdisevt_OnDisconnect(Server pServer)
{
...
}
Sponsor  
 

OSIsoft vCampus is a subscription-based, online offering that consists of providing everything people need to develop applications on the PI System.
We invite you to take a "tour" of the OSIsoft Virtual Campus - also feel free to consult the FAQ  or contact OSIsoft vCampus for more details.
RJK Solutions
#2 Posted : Wednesday, August 26, 2009 9:29:47 PM
Rank: Administration

Groups: Administration

Joined: 6/20/2008
Posts: 612
Location: Cheshire, United Kingdom.
Hi Joel,

Set IServerDisconnect to Servers collection rather than an individual server.

So

Code:

ServerDisconnect _srvdis = (IServerDisconnect) _piSDK.Servers;



Rhys.
Principal Consultant
Real-Time Data Management @ Wipro Technologies
jfaigan
#3 Posted : Thursday, August 27, 2009 1:10:55 PM
Rank: Member
Groups: Member

Joined: 8/26/2009
Posts: 10
Location: Canada
Hi,

Thanks for the reply.

I tried as you suggested but I am receiving the same error as when I used the individual Server.

Your sample shows that _srvdis is of type ServerDisconnect. Is this a typo? I do not see an object of type ServerDisconnect...only IServerDisconnect.

Regards,
Joel.
jfaigan
#4 Posted : Thursday, August 27, 2009 1:45:06 PM
Rank: Member
Groups: Member

Joined: 8/26/2009
Posts: 10
Location: Canada
I've changed the code thusly:

_DServerDisconnectEvents_Event srvdisevt = (_DServerDisconnectEvents_Event) _piSDKServer;

srvdisevt.OnDisconnect += srvdisevt_OnDisconnect;

Rather than declare them as IServerDisconnect and ServerDisconnectEvents, I am using _DServerDisconnectEvents_Event.

I am not sure what the _D classes are but they seem to work...same goes for EventPipes.

This code executes without exception. Now I just need to test if the server actually raises the event like it is supposed to.

I will let you know.

Regards,
Joel.
jfaigan
#5 Posted : Thursday, August 27, 2009 7:35:53 PM
Rank: Member
Groups: Member

Joined: 8/26/2009
Posts: 10
Location: Canada
The last code I posted allows me to receive the disconnect event.

Regards,
Joel.
RJK Solutions
#6 Posted : Thursday, August 27, 2009 9:03:50 PM
Rank: Administration

Groups: Administration

Joined: 6/20/2008
Posts: 612
Location: Cheshire, United Kingdom.
Hi Joel,

OK cool, glad you got it to work.
Strangely I ran some VBA code and the IServerDisconnect interface works a treat so must be something lost in transalation with COM.
I will follow up with OSI for completeness as to why the difference...

Cheers,

Rhys.
Principal Consultant
Real-Time Data Management @ Wipro Technologies
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.