COM / RPC disconnect problem

Discussion created by andrew_Inwood on Jun 20, 2013
Latest reply on Jun 21, 2013 by Gregor

I have a C# application in which I have a list of .PDI files.  When the user clicks on one of the items, a COM PI application Displays.Open() function call is made to load the display.  This all works fine.


The problem I have is when I try to do it a Second time.  When I go to load a file, I first enumerate through the collection of displayfiles for the application, and close them.  You can see that I have tried two method here.



public void CloseDisplays()
              for(int i=m_oPIApp.Displays.Count; i>0; i--)
              foreach (Display oDisplay in m_oPIApp.Displays)
                  oDisplay.Close(false);  //False means to not save it.



As noted, when I first start a PI application and then load a display, this code executes fine (granted, there is no display loaded when it first executes).  When I try to load a second file, I get some form of exception every time, generally some form of RPC failure, eg: 


A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in PI Display Manager.exe
The object invoked has disconnected from its clients. (Exception from HRESULT: 0x80010108 (RPC_E_DISCONNECTED))


Now, in searching for solutions to this, I found this article, relating to when COM code can fail when called a second time.


To automate Microsoft Excel, you establish an object variable that usually refers to the Excel Application object or the Excel Workbook object. Other object variables can then be set to refer to a Worksheet, a Range, or other objects in the Microsoft Excel object model. When you write code to use an Excel object, method, or property, you should always precede the call with the appropriate object variable. If you do not, Visual Basic establishes its own reference to Excel. This reference might cause problems when you try to run the automation code multiple times. Note that even if the line of code begins with the object variable, a call may be made to an Excel object, method, or property in the middle of the line of code that is not preceded with an object variable.


It seems relevant... but not 100% sure if it is.  Secondly, I am not sure how this would be applied here anyway.  I tried the m_oPIApp.Displays method (the uncommented version above) to try and address this, but it didn't solve my issue. 




OK - on a hunch, I tried making the application visible before closing the displays, and this seems to have resolved the problem.  HOWEVER - I would still appreciate any thoughts on this!