I have an application that needs to work with a vendor-supplied API to do Screen Pops when a call is routed to the user's extension. Another developer worked on getting the API to work and delivered a prototype wrapper class that isn't quite right, and I'm trying to get it right.
Because this API can block for several minutes before returning, the developer started the API interface code in a separate thread, like this:
// constructor
public ScreenPop( ...params... )
{
...
t = new Thread( StartInBackground );
t.Start();
)
private void StartInBackground()
{
_listener = new ScreenPopTelephoneListener();
_bAllocated = true;
_listener.Initialize( _address );
_bInitialized = true;
_listener.StartListening( _extension );
_bListening = true;
}
The call to Initialize is the one that could hang, if the phone server was unresponsive.
I don't think it was intentional on the part of the developer who did this, but when the API is initialized this way, the thread keeps running, so that now I have to stop the thread before I can clean up when I want to. I also have to use Invoke to get events from the thread, as others so kindly pointed out in my previous question, but that is working now.
Anyway, here is the logic used to shut everything down and clean up:
public void ShutdownFunc()
{
try
{
if ( _bListening )
{
_listener.StopListening();
_bListening = false;
}
if ( _bInitialized )
{
_listener.Shutdown();
_bInitialized = false;
}
if ( _bAllocated )
{
_listener = null;
_bAllocated = false;
}
}
}
Of course, there is a try/catch around the whole mess to prevent unhandled exceptions.
My biggest problem is, how do I call the shutdown code when the API interface code is running in a separate thread? I need to clean up in case the user logs off and then logs back on again, as the API gets confused if I don't. Thread.Abort won't work, as it's not in a place to catch the exception, and the "volatile bool" method won't work either, for obvious reasons (most of the time, the API thread is not active).
So there seems to be no way to call the shutdown logic if the API interface code is running on a separate thread. Thread.Abort() bypasses all the logic and kills the thread without cleaning up. Calling the shutdown logic from the GUI thread hangs when it calls the Shutdown method. So, what can I do?
The IDispose interface just adds another layer of abstraction in the GUI thread, and does nothing to solve the problem here.
For now, to get the rest of the code developed, I am eliminating threads completely. But, before I release the app, I need to figure this out.
Anybody?
Thanks,
Dave