Thank you. Threading is STA. Here is an excert of the code that works
fine as a windows form... But, for some reason I can not put it
together to work as a service.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Runtime.InteropServices;
using Envox.ADXVoice;
using System.Data.SqlClient;
using System.Configuration;
namespace AnsweringSystem
{
public partial class Form1 : Form
{
private LineHandler[] LineArray;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int iFromChannel = 0;
int iToChannel = 3; //4 channels
LineArray = new LineHandler[iToChannel - iFromChannel + 1];
for (int iChannel = iFromChannel; iChannel <= iToChannel;
iChannel++)
{
LineHandler Line = new LineHandler();
LineArray[iChannel - iFromChannel] = Line;
Line.iChannel = iChannel;
ThreadStart ts = new ThreadStart(Line.ProcessingThread);
Thread wrkThread = new Thread(ts);
Line.CurrentThread = wrkThread;
wrkThread.SetApartmentState(ApartmentState.STA);
wrkThread.Name = iChannel.ToString();
wrkThread.Start();
}
}
//Main class to handle individual channels on separate threads
private class LineHandler
{
public int iChannel = -1;
public Thread CurrentThread = null;
private ADXVoiceClass ADXVoice1 = null;
private delegate void TraceThreadSafe(string msg);
public void ProcessingThread()
{
try
{
ADXVoice1 = new ADXVoiceClass();
ADXVoice1.TrunkAssign(iChannel);
ADXVoice1.IncomingCall += new
_IADXVoiceEvents_IncomingCallEventHandler(OnIncomingCall);
}
catch (COMException e)
{
ADXVoice1 = null;
return;
}
catch (Exception e)
{
ADXVoice1 = null;
return;
}
//Start message loop (keeps thread from exiting and allows
ADX to process events, including state changes)
Application.Run();
Marshal.ReleaseComObject(ADXVoice1); //make ADXVoice1
eligible for garbage collection
GC.Collect(); //force Garbage Collection
GC.WaitForPendingFinalizers(); //wait for completion
}
private void OnIncomingCall()
{
//code here to play prompts, branch to other methods, etc.
}
} //End LineHandler class
}
}
I very much appreciate any advice you may have. Thank you very much.
It looks like you are using an ActiveX Component, now AX was in general
not designed to be used from a service, make them work is in general a
pain in the a**, the problem is with the callbacks that arrive on
anything but the callers thread, that is they end on the thread pool
threads which are MTA and as such they need to be marshaled to the STA
thread, that means you need to pump Windows messages (not COM message),
but services are not designed to pump a message queue, only Windows
applications are, you see the dilemma?
I would suggest you to try to run this from a Console application first,
it's much easier to debug, and look how far you get before you drop this
into a service.
Willy.