We have an internally written program in C# that use RDO in Outlook Redemption to create, modify, or delete contacts in an Outlook Contacts folder. This is Outlook 2019 connected to on-premise Exchange. The program is successful is creating, modifying or deleting contacts. However, if Outlook is not running, when the C# program tries to exit, it hangs. I can see the process is idle in Task Manager. Even if I wait overnight, the program never exits.
If I run the C# program with Outlook running, the program runs to completion, doing what it needs to do, and exits successfully.
What do I need to change, whether it's in the C# code, in Outlook settings, or in Windows settings, that will allow the program to exit cleanly?
CodePudding user response:
This is a guess, as you don't share enough code. But I would suspect that your program is not disposing the resources. Make sure "using" is being used wherever you create the RDO objects, to make sure your program is not leaking them
CodePudding user response:
Yes, most likely you are not making sure all Redemption objects and RDOSession
in particular, are not being released on the main thread.
Before shutdown, make sure all global Redemption objects are either explicitly released by calling Marshal.ReleaseComObject
or go out of scope. In the latter case, call GC.Collect() (to make sure all out-of-scope objects are immediately released) before releasing RDOSession with Marshal.ReleaseComObject
.
Worst case, try calling RDOSession.DoFastShutdown()
before closing.
CodePudding user response:
Outlook programming (and Redemption) is based on dealing with underlying COM objects. You need to keep that in mind when dealing with Outlook or Redemption.
To deal with underlying COM objects correctly I'd recommend using the ReleaseComObject method of the Marshal
class which decrements the reference count of the specified Runtime Callable Wrapper (RCW) associated with the specified COM object, however it doesn't release an object. So, to release the object the number of increment and decrement operations should be equal.
Typically you need to release every object returned from the Office (Outlook or Redemption in your case) object model. Exceptions are objects passed to event handlers as parameters.
Also you may consider using the following sequence of calls to release objects:
GC.Collect
GC.WaitForPendingFinalizers
GC.Collect
GC.WaitForPendingFinalizers
As soon as all underlying COM objects are released the managed application can be closed. You may read more about that and find answers to your multiple questions in the When to release COM objects in Office add-ins developed in .NET article.