Search code examples
c#eventstapi

Events corrupts the result of the function


Hello fellow programmers

I'm writing a program to C # which manages TAPI library. I will not give the code (unless someone requests it) because it has commands for a software called Softone and is business related. The problem I'm facing is the following:

I want for example when a call comes to perform a function. Quite simple. So I set the event hanlder and call the function but while it is running the result it gives to the program I mentioned (Softone) is wrong. The same function whether executed manually or in any other way gives correct results. I tried to disable the event handler, execute the function, and re-activate the event handler. But this is also a failure.

What I would like is another way to prevent events from falsifying the data of he function. Do you have anything to suggest?

I'm a beginner so please show understanding. Thanks :)

        static public void RegisterTapi()
    {
        tapi.Initialize();
        tapi.EventFilter = (int)(
        TAPI3Lib.TAPI_EVENT.TE_CALLNOTIFICATION |
        TAPI3Lib.TAPI_EVENT.TE_CALLINFOCHANGE |
        TAPI3Lib.TAPI_EVENT.TE_DIGITEVENT |
        TAPI3Lib.TAPI_EVENT.TE_PHONEEVENT |
        TAPI3Lib.TAPI_EVENT.TE_CALLSTATE |
        TAPI3Lib.TAPI_EVENT.TE_GENERATEEVENT |
        TAPI3Lib.TAPI_EVENT.TE_GATHERDIGITS |
        TAPI3Lib.TAPI_EVENT.TE_REQUEST);

        tapi.ITTAPIEventNotification_Event_Event += new TAPI3Lib.ITTAPIEventNotification_EventEventHandler(TapiCall.tapi_ITTAPIEventNotification_Event_Event);
    }



        public static void tapi_ITTAPIEventNotification_Event_Event(TAPI3Lib.TAPI_EVENT TapiEvent, object pEvent)
        {


            switch (TapiEvent)
            {
                case TAPI3Lib.TAPI_EVENT.TE_CALLNOTIFICATION: 

                ITCallNotificationEvent tcallNotificationEvent = (TAPI3Lib.ITCallNotificationEvent)pEvent;
                TAPI3Lib.ITCallInfo a = tcallNotificationEvent.Call;

                switch (a.CallState)
                {
                    case TAPI3Lib.CALL_STATE.CS_OFFERING://A new call has appeared
                        tapi.ITTAPIEventNotification_Event_Event -= TapiCall.tapi_ITTAPIEventNotification_Event_Event;
                        ActionOffering();
                        tapi.ITTAPIEventNotification_Event_Event += TapiCall.tapi_ITTAPIEventNotification_Event_Event;
                        break; 
                }
                break;
            }
            break;
        }


    public static void ActionOffering() 
    {
        string sqa_action = *SQL QUERY*
        XTable ds_action = XSupport.GetSQLDataSet(sqa_action, null);
        if (ds_action.Count > 0)
        {
            string caller_action = ds_action.Current["ACTION"].ToString();

            XSupport.ExecS1Command(caller_action, null);
        }
    }

Solution

  • As FelixCastor suggested, I checked the thread in which the function I'm calling is running and did not run on the same thread. The change I made in the code was very small.

    I declared the dispatcher in the code section that I know will be executed by the main thread.

     public static Dispatcher dispatcher = Dispatcher.CurrentDispatcher;
    

    I did this because, according to the dοcumentation the dispatcher will "run" on the thread that is declared. So if I wanted to run the function on the main thread, I had to declared it there. Then i wrote this simple line of code that forces the ActionOffering function to run on the dispatcher (main) thread.

    dispatcher.BeginInvoke(new InvokeDelegate(ActionOffering));