0

我有这个问题。我想通过 Skype 拨打各种号码。为了使其正常工作,我必须先拨打电话,然后等待此电话完成(在收到铃声后挂断),然后再拨打另一个电话。

问题是,虽然我尝试异步执行并等待函数返回,但我收到一个错误,表明下一个调用在第一个调用完成之前开始。这是代码:

class Program
{
  static Skype skype = new Skype();
  static String[] numbers = new String[] { "phonenumber1", "phonenumber2" };
  public delegate void TCall(Call pCall, TCallStatus Status);

static void Main(string[] args)
    {

        ((_ISkypeEvents_Event)skype).AttachmentStatus += OnAttachmentStatus;


        skype.Attach(8);

        Console.ReadLine();
    }

    static void OnAttachmentStatus(TAttachmentStatus status)
    {
        Console.WriteLine("attacment: " + skype.Convert.AttachmentStatusToText(status));


        // only after this apiAttachSuccess can we do much anything
        if (status == TAttachmentStatus.apiAttachSuccess)
        {
            foreach (string number in numbers)
            {
                Call pCall = skype.PlaceCall(number);
                TCall theCall = OnCallStatus;
                IAsyncResult ar = theCall.BeginInvoke(pCall, pCall.Status, null, null);
                theCall.EndInvoke(ar);
                while (!ar.IsCompleted)
                {
                    Thread.Sleep(200);
                }

                Console.WriteLine("Next Call Please");
            }

        }
    }

    static void OnCallStatus(Call pCall, TCallStatus Status)
    {
        //Thread.Sleep(2000);

        var sw = Stopwatch.StartNew();
        while (sw.ElapsedMilliseconds<=5000)
        {
            if (pCall.Status == TCallStatus.clsRinging)
            {
                Console.WriteLine("Ringing");
                break;

            }
        }
        return;



    }
}

我究竟做错了什么?

4

1 回答 1

0

我认为您调用 EndInvoke 为时过早。尝试在 while 循环之后移动它。

或者,您可以使用这样的等待句柄

IAsyncResult ar = theCall.BeginInvoke(pCall, pCall.Status, null, null);
ar.AsyncWaitHandle.WaitOne();  //Blocks until complete
theCall.EndInvoke(ar);
于 2013-05-29T17:18:10.663 回答