2

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
            }
        }
    }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Ryan Hargreaves
  • 267
  • 3
  • 16

1 Answers1

2

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.

NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • The code is in a separate class. That code is throwing a compile-time error: 'TAPI' does not contain a definition for 'InvokeRequired' and no extension method 'InvokeRequired' accepting a first argument of type 'TAPI' could be found (are you missing a using directive or an assembly reference?) – Ryan Hargreaves May 16 '16 at 11:33
  • Does the class have some reference to the main form or some other open form availble? Then use that instead of this. Or use Application.OpenForms[0] – NineBerry May 16 '16 at 11:36
  • Lovely, that has fixed the issue. I didn't realise UI had to be called from the main thread. Thanks :) – Ryan Hargreaves May 16 '16 at 11:39
  • One can have multiple threads with UI in a winforms application, but these other threads need to include their own message loop for the UI to work correctly. – NineBerry May 16 '16 at 11:44