Search code examples
c#winformstapi

Form not loading correctly


I'm trying to build a TAPI based phone call system using JulMar's Atapi x86. One of the functions is to pop a specific form on an inbound call. However, whenever the form pops, it comes up incorrect, as shown below (I have tried several forms as a test and they all do the same thing). There is no error, nothing in the output window to suggest what the issue is.

Form when I try to load

Code:

private void incomingcall(object sender, NewCallEventArgs e)
    {
        string phonenumber = e.Call.CallerId; //get the phone number of the call
        SqlCommand getincoming = new SqlCommand(Querystrings.getincomingquery(), DB);
        getincoming.Parameters.AddWithValue("@@TELEPHONE", phonenumber);
        DataTable results = new DataTable(); 
        try
        {
            DB.Open();
            using (var results = getincoming.ExecuteReader())
            {
                results.Load(results);
            }
        }
        catch (Exception ex)
        {
            Inbound ib = new Inbound(phonenumber, null);
            ib.Show();
        }
        finally
        {
            DB.Close();
        }
        if (results.Rows.Count == 1)
        {
            loadcontactrequest(Convert.ToInt32(results.Rows[0].ItemArray[0]), phonenumber);
        }
        else
        {
            loadinbound(phonenumber, results);
        }
    }

I have loaded these forms outside of this function at other points, meaning it is something to do with this function. Does anybody know where I'm going wrong?

EDIT:

private void loadcontactrequest(int ContactID, string phonenumber)
    {
        ContactRequest cr = new ContactRequest(ContactID, Global.loginbound("Single customer found", phonenumber));
        cr.Show();
    }

These functions have been tested elsewhere and work correctly individually, I believe it might be TAPI related.

EDIT 2 - Delegate:

public static void inittapi()
    {
        if (TestOptions.notapi)
            return;
        tapi = new TapiManager("Omitted");
        tapi.Initialize();
        foreach (TapiLine ad in tapi.Lines) //Get all lines available to this PC
        {
            if (ad.Name.ToUpper().Contains("Omitted")) 
            {
                phoneline = ad;
                phoneline.Open(MediaModes.All); //Open the phone line for making and receiving calls
                phoneline.NewCall += new EventHandler<NewCallEventArgs>(new TAPI().incomingcall); //Add the incoming call event handler
            }
        }
    }

Solution

  • It's possible that this event is triggered on a different thread than the UI thread of your application.

    Modify the method like this to test whether this is the problem:

    private void incomingcall(object sender, NewCallEventArgs e)
    {
         Form form;
    
         if(Application.OpenForms.Count > 0)
         {
              form = Application.OpenForms[0];
         }
    
         if (form != null && form.InvokeRequired)
         {
              form.BeginInvoke(new Action(() => { incomingcall(sender, e); }));
              return;
         }
    
         // Your current code goes here
    }
    

    This will identify that we are in a different thread than your main form (form) was created on and then execute the function again on the main form's thread.