我有这个问题。我想通过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;
}
}
我做错了什么?
答案 0 :(得分:0)
我认为你过早地调用EndInvoke。尝试在while循环后移动它。
或者,您可以使用像这样的等待句柄
IAsyncResult ar = theCall.BeginInvoke(pCall, pCall.Status, null, null);
ar.AsyncWaitHandle.WaitOne(); //Blocks until complete
theCall.EndInvoke(ar);